配置与启用

任务配置参数,可以直接在对应的 Server->setting 配置即可,如果要启用任务更简单,Server 新增一个 on 事件。

Http Server 配置开启任务为例:

  1. return [
  2. 'httpServer' => [
  3. // ...
  4. 'on' => [
  5. SwooleEvent::TASK => \bean(TaskListener::class), // Enable task must task and finish event
  6. SwooleEvent::FINISH => \bean(FinishListener::class)
  7. ],
  8. /* @see HttpServer::$setting */
  9. 'setting' => [
  10. 'task_worker_num' => 12,
  11. 'task_enable_coroutine' => true
  12. ]
  13. ],
  14. ]

Rpc Server 配置开启任务为例:

  1. return [
  2. 'rpcServer' => [
  3. // ...
  4. 'on' => [
  5. SwooleEvent::TASK => \bean(TaskListener::class), // Enable task must task and finish event
  6. SwooleEvent::FINISH => \bean(FinishListener::class)
  7. ],
  8. /* @see HttpServer::$setting */
  9. 'setting' => [
  10. 'task_worker_num' => 12,
  11. 'task_enable_coroutine' => true
  12. ]
  13. ],
  14. ]

wsServer Server 配置开启任务为例:

  1. return [
  2. 'wsServer' => [
  3. // ...
  4. 'on' => [
  5. SwooleEvent::TASK => \bean(TaskListener::class), // Enable task must task and finish event
  6. SwooleEvent::FINISH => \bean(FinishListener::class)
  7. ],
  8. /* @see HttpServer::$setting */
  9. 'setting' => [
  10. 'task_worker_num' => 12,
  11. 'task_enable_coroutine' => true
  12. ]
  13. ],
  14. ]

任务配置与启用,在 Http Server / Rpc Server / Websocket Server 都完全一样,启用任务需要监听 task finish 两个事件。