## 安裝
~~~
composer require easyswoole/task
~~~
## 獨立使用示例
~~~php
use EasySwoole\Task\Config;
use EasySwoole\Task\Task;
/*
配置項中可以修改工作進程數、臨時目錄,進程名,最大并發執行任務數,異常回調等
*/
$config = new Config();
$task = new Task($config);
//添加swoole 服務
$http = new swoole_http_server("0.0.0.0", 9501);
//注入swoole服務,進行創建task進程
$task->attachToServer($http);
//在onrequest事件中調用task(其他地方也可以,這只是示例)
$http->on("request", function (Swoole\Http\Request $request, $response)use($task){
if(isset($request->get['sync'])){
//同步調用task
$ret = $task->sync(function ($taskId,$workerIndex){
return "{$taskId}.{$workerIndex}";
});
$response->end("sync result ".$ret);
}else if(isset($request->get['status'])) {
var_dump($task->status());
}else{
//異步調用task
$id = $task->async(function ($taskId,$workerIndex){
\co::sleep(1);
var_dump("async id {$taskId} task run");
});
$response->end("async id {$id} ");
}
});
//啟動服務
$http->start();
~~~