Use the task function provided by swoole in php-fpm or apache.

  php, redis, swoole, task

AsyncTask is a set of producer-consumer model provided by swoole, which can easily deliver a slow task to the queue and be executed asynchronously by the process pool. Task function is currently only available in swoole_server. Version 1.9.0 provides the RedisServer framework. A server program can be implemented based on RedisServer and task. swoole Task function can be used by directly calling Redis extension in php-fpm or apache.

Create RedisServer

<?  php
 use Swoole\Redis\Server;
 
 $server = new Server("127.0.0.1", 9501, SWOOLE_BASE);
 
 $server->set(array(
 'task_worker_num' => 32,
 'worker_num' => 1,
 ));
 
 $server->setHandler('LPUSH', function ($fd, $data) use ($server) {
 $taskId = $server->task($data);
 if ($taskId === false)
 {
 return Server::format(Server::ERROR);
 }
 else
 {
 return Server::format(Server::INT, $taskId);
 }
 });
 
 $server->on('Finish', function() {
 
 });
 
 $server->on('Task', function ($serv, $taskId, $workerId, $data) {
 //Handling tasks
 });
 
 $server->start();
  • If it is a native call, UnixSocket can be monitored, and IP:PORT is required for local area network calls.

  • Task$dataIs the data delivered by the client

  • Redis clients can also be used to deliver tasks in other languages.

  • It can be adjusted according to the speed of Task execution.task_worker_numControls the number of processes started. These processes are managed by the swoole bottom layer, which recreates new task processes after a fatal error or process exits.

Delivery task

$redis = new Redis;
$redis->connect('127.0.0.1', 9501);
$taskId = $redis->lpush("myqueue", json_encode(array("hello", "swoole")));

Note that this RedisServer is not a real Redis server, it only supportsLPUSHAn instruction.