At this stage, `Swoole` has no way to `hook` all blocking functions, which means that some functions will still cause `process blocking`, which will affect the scheduling of coroutines. At this time, we can simulate coroutines by using the `Task` component. In order to achieve the purpose of calling blocking functions without blocking the process, in essence, it is still multi-process running blocking functions, so the performance will be obviously inferior to the native coroutine, depending on the number of `Task Worker`.
## Install
```bash
composer require hyperf/task
```
## Configure
Because Task is not the default component, you need to add `Task` related configuration to `server.php` when using it.
```php
<?php
declare(strict_types=1);
use Hyperf\Server\Event;
return [
// Other irrelevant configuration items are omitted here
'settings' => [
// Number of Task Workers, configure the appropriate number based on your server configuration
'task_worker_num' => 8,
// Because `Task` mainly deals with methods that cannot be coroutined, it is recommended to set `false` here to avoid data confusion under coroutines
It is not particularly intuitive to use `active method delivery`. Here we implement the corresponding `#[Task]` annotation and rewrite the method call through `AOP`. When in the `Worker` process, it is automatically delivered to the `Task` process, and the coroutine waits for the data to return.
| workerId | int | -1 | Specifies the process ID of the task to be delivered (-1 means random delivery to an idle process) |
## Appendix
Swoole does not have a list of coroutine functions for the time being
- mysql, the bottom layer uses libmysqlclient, which is not recommended, it is recommended to use pdo_mysql/mysqli that has already implemented coroutines
- mongo, the bottom layer uses mongo-c-client
- pdo_pgsql
- pdo_ori
- pdo_odbc
- pdo_firebird
### MongoDB
> Because `MongoDB` has no way to be `hook`, we can call it through `Task`. The following is a brief introduction to how to call `MongoDB` through annotations.
Below we implement two methods `insert` and `query`. It should be noted that the `manager` method cannot use `Task`,
Because `Task` will be processed in the corresponding `Task process`, and then return the data from the `Task process` to the `Worker process`.
Therefore, the input and output parameters of the `Task method` should not carry any `IO`, such as returning an instantiated `Redis` and so on.
```php
<?php
declare(strict_types=1);
namespace App\Task;
use Hyperf\Task\Annotation\Task;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Query;
use MongoDB\Driver\WriteConcern;
class MongoTask
{
public Manager $manager;
#[Task]
public function insert(string $namespace, array $document)
{
$writeConcern = new WriteConcern(WriteConcern::MAJORITY, 1000);
If the Task mechanism cannot meet the performance requirements, you can try another open source project under the Hyperf organization [GoTask](https://github.com/hyperf/gotask). GoTask starts the Go process as the Swoole main process sidecar through the Swoole process management function, and uses the process communication to deliver the task to the sidecar for processing and receive the return value. It can be understood as the Go version of Swoole TaskWorker.