Configuration and enable

Task configuration parameters can be configured directly in the corresponding Server->setting . If you want to enable the task more easily, Server adds an on event.

Coroutine task

The Http Server configuration open task is an example:

 return [
    '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
        ]
    ],
] 
  • task_enable_coroutine must be true
  • The task event and the finish event must be configured and are TaskListener::class and FinishListener::class

Rpc Server configuration open task as an example:

 return [
    'rpcServer' => [
        // ...
        '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
        ]
    ],
] 

The wsServer Server configuration open task is an example:

 return [
    'wsServer' => [
        // ...
        '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
        ]
    ],
] 

The task configuration and enablement are exactly the same in Http Server / Rpc Server / Websocket Server . To enable the task, you need to listen to the task finish two events.

Synchronous blocking task

Swoft not only provides coroutine tasks, but also supports synchronous tasks. Synchronous tasks and coroutine tasks can only choose one type of operation, and the two cannot exist at the same time. Synchronous tasks only need to configure task events, and do not support asynchronous finish events. The official recommendation is to use coroutine tasks to implement the business. If you need to implement MongoDB and PostgreSQL through tasks like this, use the synchronization task.

2.0.4+ support

As an example of Http-server:

 return [
    'httpServer' => [
        // ...
        'on'       => [
            SwooleEvent::TASK   => bean(SyncTaskListener::class),  // Enable sync task
        ],
        /* @see HttpServer::$setting */
        'setting'  => [
            'task_worker_num'       => 6,
            'task_enable_coroutine' => false
        ]
    ],
] 
  • task_enable_coroutine must be set to false
  • The task event must be SyncTaskListener::class

Synchronous blocking tasks, you can not directly use all the IO operations (database, cache, RPC, etc.) provided by the framework and application logs, controller logs can be used. The definition and use of synchronous blocking tasks is the same as for coroutine tasks, but without context.

/docs/2.x/en/task/setting.html
progress-bar