介紹swoft中
1、Task
2、協程
一:Task任務:
1、配置,在 app/bean.php文件中加入

'httpServer' => [ // ... 'on' => [ SwooleEvent::TASK => \bean(TaskListener::class), // Enable task must task and finish event SwooleEvent::FINISH => \bean(FinishListener::class) ], /* @see HttpServer::$setting */ 'setting' => [ 'task_worker_num' => 12, 'task_enable_coroutine' => true, 'worker_num' => 2 ] ],
2、定時任務

1、安裝 composer require swoft/crontab 2、配置 'httpServer' => [ // ... 'process' => [ 'crontab' => bean(Swoft\Crontab\Process\CrontabProcess::class) ], // ... ], 3、定義,在/app/Task/Crontab/ 文件夾中新建文件 <?php namespace App\Task\Crontab; use Swoft\Crontab\Annotaion\Mapping\Cron; use Swoft\Crontab\Annotaion\Mapping\Scheduled; /** * Class DemoCronTask * @package App\Task\Crontab * * @Scheduled(name="demoCronTask") //聲明定時任務 */ class DemoCronTask{ /** * @Cron("*") //每秒執行 */ public function secondTask(){ var_dump("--111----",date('Y-m-d H:i:s', time())); } /** * @Cron("0 * * * * *") //每分鍾執行 */ public function miunteTask(){ var_dump("222------",date('Y-m-d H:i:s', time())); } }
3、協程、異步任務
(1)、聲明一個任務,在 /app/Task/Task/ 文件夾新建文件

<?php declare(strict_types=1); namespace App\Task\Task; use Swoft\Task\Annotation\Mapping\Task; use Swoft\Task\Annotation\Mapping\TaskMapping; /** * Class DemoTask * * @since 2.0 * @Task(name="demoV2Task") //標記類是一個任務 */ class DemoTask { /** * @TaskMapping(name="list") //映射名稱 * * @param int $id * @param string $default * * @return array */ public function getList(int $id): array { var_dump("------------"); sleep(5); return [ 'list' => [1, 3, 3], 'id' => $id ]; } /** * @param int $id * @return bool * * @TaskMapping(name="putLists") */ public function putList(int $id) : bool { if($id > 5) return true; return false; } }
(2)、任務投遞
// 協程投遞 $data = Task::co('demoV2Task', 'list', [12]); //異步投遞 $data = Task::async('demoV2Task', 'list', [12]);
(3)、異步投遞如果需要關注異步任務處理結果,可以添加監聽器,在文件夾 /app/Task/Listener/ 下新建文件

<?php namespace App\Task\Listener; use Swoft\Log\Helper\CLog; use function context; use Swoft\Event\Annotation\Mapping\Listener; use Swoft\Event\EventHandlerInterface; use Swoft\Event\EventInterface; use Swoft\Task\TaskEvent; /** * Class DemoListener * @package App\Task\Listener * @Listener(event=TaskEvent::FINISH) //參數必須帶Finsh */ class DemoListener implements EventHandlerInterface{ /** * @param EventInterface $event * * @throws \Swoft\Exception\SwoftException */ public function handle(EventInterface $event): void { // TODO: Implement handle() method. $fId = context()->getTaskUniqid(); // var_dump($fId); CLog::info(\context()->getTaskUniqid()); $taskData = context()->getTaskData(); // var_dump($taskData); CLog::info(\context()->getTaskData()); } }
二:協程 : https://www.swoft.org/docs/2.x/zh-CN/common/co.html
use Swoft\Co;
//創建一個協程 Co::create(function(){ // to do sleep(3); var_dump("--2222----"); }); //並發 $request = [ 'method' => function () { sleep(8); return "111"; }, 'staticMethod' => function () { sleep(5); return "2222"; }, 'closure' => function () { sleep(2); return "3333"; } ]; $resd = Co::multi($request);//同時執行,同步,會阻塞
注意:Task中有兩個地方還未清楚
1、協程投遞任務時是阻塞( $data = Task::co('demoV2Task','list',[2],10); )。
2、使用 Co::multi() 執行並發時會阻塞。
3、異步監聽的地方,所有監聽器只有帶了參數 @Listener(event=TaskEvent::FINISH) 都會執行一遍。
查看文檔:
Task : https://www.swoft.org/docs/2.x/zh-CN/task/index.html
協程 : https://www.swoft.org/docs/2.x/zh-CN/common/co.html