Process pool

The process pool is generally used for scenarios that require the program to run all the time, such as queue consumption and data calculation. In the Swoft framework, it is re-encapsulated based on the Swoole process pool model, making it easy for developers to quickly and easily use the process pool.

2.0.4+ support and need to install the swoft-process component

Configuration

After the component is successfully installed, it does not need to be configured or used by default. The configuration is as follows:

App/bean.php

 return [
    'processPool' => [
        'class' => \Swoft\Process\ProcessPool::class,
        'workerNum' => 3
    ]
]; 

The detailed parameters are as follows:

  • workerNum worker number of processes
  • ipcType IPC type
  • Whether coroutine , the default is to enable

The Swoft framework must be running in coroutine mode. In coroutine mode, all IO operations encapsulated by Swoft can be used, as well as other non-Swoft coroutine operations.

annotation

@Process

@Process tag class is a process pool process

Property list:

  • workerId int|array binds the process ID, which can be single or array. By default, it is bound to other unbound processes.

Declare the work process

Once configured, the work process is declared. For example, workerNum = 3, define three worker processes as an example:

Worker process 1:

 <?php declare(strict_types=1);

namespace App\Process;

use Swoft\Log\Helper\CLog;
use Swoft\Process\Annotation\Mapping\Process;
use Swoft\Process\Contract\ProcessInterface;
use Swoole\Coroutine;
use Swoole\Process\Pool;

/**
 * Class Worker1Process
 *
 * @since 2.0
 *
 * @Process(workerId=0)
 */
class Worker1Process implements ProcessInterface
{
    /**
     * @param Pool $pool
     * @param int  $workerId
     */
    public function run(Pool $pool, int $workerId): void
    {
        while (true) {
            CLog::info('worker-' . $workerId);

            Coroutine::sleep(3);
        }
    }
} 
  • The worker process must implement the Swoft\Process\Contract\ProcessInterface interface.
  • Developer business must implement while(true) like while(true) logic
  • @Process Annotation workerId=0 indicates that the first process is bound to this processing logic flow.

Worker process 2 and process 3:

 <?php declare(strict_types=1);

namespace App\Process;

use App\Model\Entity\User;
use Swoft\Db\Exception\DbException;
use Swoft\Log\Helper\CLog;
use Swoft\Process\Annotation\Mapping\Process;
use Swoft\Process\Contract\ProcessInterface;
use Swoft\Redis\Redis;
use Swoole\Coroutine;
use Swoole\Process\Pool;

/**
 * Class Worker2Process
 *
 * @since 2.0
 *
 * @Process(workerId={1,2})
 */
class Worker2Process implements ProcessInterface
{
    /**
     * @param Pool $pool
     * @param int  $workerId
     *
     * @throws DbException
     */
    public function run(Pool $pool, int $workerId): void
    {
        while (true) {

            // Database
            $user = User::find(1)->toArray();
            CLog::info('user='.json_encode($user));

            // Redis
            Redis::set('test', 'ok');
            CLog::info('test='.Redis::get('test'));

            CLog::info('worker-' . $workerId.' context='.context()->getWorkerId());

            Coroutine::sleep(3);
        }
    }
} 
  • The worker process must implement the Swoft\Process\Contract\ProcessInterface interface.
  • Developer business must implement while(true) like while(true) logic
  • @Process Annotation workerId={1,2} indicates the second process and the third process, and binds this processing logic flow at the same time.

workerId binding ID is from 0. If the workerId is not written by default, the current program flow is bound to other unbound processes.

run

After the configuration and declaration work process is completed, the process pool is started. The startup process pool is similar to other service startups.

Non-background startup:

 php bin/swoft process:start 

Start in the background:

  [root@swoft swoft]# php bin/swoft process:start -d
 2019/07/16-09:44:34 [INFO] Swoft\SwoftApplication:setSystemAlias(496) Set alias @base=/data/www/swoft
 ... ...
 2019/07/16-09:44:44 [INFO] Swoft\Processor\ConsoleProcessor:handle(39) Console command route registered (group 13, command 40)
 

Restart all worker processes:

 [root@swoft swoft]# php bin/swoft process:reload 
2019/07/16-09:45:52 [INFO] Swoft\SwoftApplication:setSystemAlias(496) Set alias @base=/data/www/swoft
... ...
2019/07/16-09:45:59 [INFO] Swoft\Processor\ConsoleProcessor:handle(39) Console command route registered (group 13, command 40)
Server bin/swoft is reloading
Process pool bin/swoft reload success
 

Restart:

  [root@swoft swoft]# php bin/swoft process:restart      
 2019/07/16-09:45:28 [INFO] Swoft\SwoftApplication:setSystemAlias(496) Set alias @base=/data/www/swoft
 ... ...
 2019/07/16-09:45:37 [INFO] Swoft\Processor\ConsoleProcessor:handle(39) Console command route registered (group 13, command 40)
 Stopping .. Successful!
 Process pool restart success !
 

Out of service:

  [root@swoft swoft]# php bin/swoft process:stop  
 2019/07/16-09:46:35 [INFO] Swoft\SwoftApplication:setSystemAlias(496) Set alias @base=/data/www/swoft
 ... ...
 2019/07/16-09:46:45 [INFO] Swoft\Processor\ConsoleProcessor:handle(39) Console command route registered (group 13, command 40)
 Stopping .. Successful! 
/docs/2.x/en/process/process-pool.html
progress-bar