框架获取
funtask
更新日志
- 2018-07-29 Swoole1.0简单版生产消费模型,采用多进程模式
- 2019-04-25 采用Swoole4重构进程池(退出重启),增加进程组(退出不重启)
- 2019-05-06 支持协程Coroutine
使用例子
耗时任务例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Consumer implements \Funsoul\Funtask\Process\JobInterface {
public function handle(): bool { $pid = getmypid();
$i = 0; $running = true; while ($running) { echo "{$pid}: " . $i++ . PHP_EOL;
if ($i == 5) $running = false; }
return false; } }
|
ProcessPool进程池
1 2 3 4 5 6 7
| $task = new \Funsoul\Funtask\Funtask(); $task->setType('POOL') ->setJob(new Consumer()) ->setWorkerNum(3) ->setWorkerName('myWorker') ->start();
|
ProcessGroup进程组
1 2 3 4 5 6
| $task = new \Funsoul\Funtask\Funtask(); $task->setType('GROUP') ->setJob(new Consumer()) ->setWorkerNum(3) ->setWorkerName('myWorker') ->start();
|
Coroutine协程
协程任务例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class ConsumerCo implements \Funsoul\Funtask\Coroutine\CoJobInterface {
public function handle(\Swoole\Http\Request $request) { $cid = Co::getuid();
$i = 0; $running = true; while ($running) { echo "{$cid}: " . $i++ . PHP_EOL;
Co::sleep(1);
if ($i == 5) $running = false; } } }
|
funtask统一接口
1 2 3 4 5 6 7
| $task = new \Funsoul\Funtask\Funtask(); $task->setType('CO')->setCoJob(new ConsumerCo())->setWorkerNum(3);
$task->setFinishCallback(function ($response) { $response->end('finished'); }); $task->start();
|
coroutine接口
1 2 3 4 5 6 7 8 9 10 11
| $co = new \Funsoul\Funtask\Coroutine\Coroutine(); $co->setHost('127.0.0.1') ->setPort(9501) ->setWorkerNum(1) ->setCoNum(3) ->setJob(new ConsumerCo());
$co->start(function ($response) { $response->end("finished!\n"); });
|
不为赚钱,只为交个朋友!干杯~
WeChat Pay
Alipay
Welcome to my other publishing channels