承接 alicemajere/wonderland-thread 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alicemajere/wonderland-thread

Composer 安装命令:

composer require alicemajere/wonderland-thread

包简介

Simple multi-threading management library

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage Code Intelligence Status License

Wonderland Thread

A small simple multi-threading library to include in projects

Recent Update

The project just got released on packagist with release 1.0.0.

Installation

You need to have composer and autloader installed. To be able to install the package, add this entry in the "repositories" index of your composer.json

      {
        "type": "vcs",
        "url":  "git@github.com:AliceMajere/wonderland-thread.git"
      }

Just require the package with composer

composer require alicemajere/wonderland-thread

Usage

To start a multi-threading Pool, just create a new instance of ThreadPool

$threadPool = new ThreadPool(); 

We setup a maximum number of Thread to run at the same time. If we add 200 Threads to the Pool, only 5 of them will run at the same time until the Pool processed all the 200 Threads

$threadPool->setMaxRunningThreadNb(5);

Add one or more Thread to the Pool by creating a new instance of Thread. A Thread take two parameters, a name and a closure function that will tell the Thread what to do. The closure have to return an exit status from the ones defined in the Thread class constants.

$thread = ThreadFactory::create(
    'ThreadName',
    function ($processName) {
        // Implements the Thread processing here
        echo $processName . PHP_EOL;
        
        // return an exit status at the end of the thread
        return Thread::EXIT_STATUS_SUCCESS;
    }
)

$threadPool->addThread($thread);

To run the ThreadPool, you can just do

$threadPool->run();

You can add Listeners that will trigger on a particular Event while the Pool is running the Threads. Using Listeners can be useful if you need to do particular things for every Threads, like opening a separate mysql connection for every Thread. A Listener object constructor need the Event name to listen to and a closure function that will tell the Listener what to do for this Event. The full list of Event definition is found in the Event class.

$threadPool->addListener(new Listener(
    Event::POOL_RUN_START, // Event to trigger the listener
    function (Event $event) use ($website) {
        // Implements the listener
    })
);
$threadPool->addListener(new Listener(
    Event::POOL_RUN_STOP,
    function (Event $event) {
        $this->io->progressFinish();
    })
);

Prerequisites

PHP >= 7.2

Getting help

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

Authors

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固