## 安装
~~~
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();
~~~