swoole task 的应用。
swoole 异步task,主要实现调用异步任务的执行。 (推荐学习: swoole视频教程)
常用的场景:异步支付处理、异步订单处理、异步日志处理、异步发送邮件/短信等。
swoole 的实现方式是 worker 进程处理数据请求,分配给 task 进程执行。
官方介绍:
task 底层使用unix socket管道通信,是全内存的,没有io消耗。单进程读写性能可达100万/s,不同的进程使用不同的管道通信,可以最大化利用多核。
本地版本:php 7.2.6、swoole 4.3.1。
不多说,先看效果图:
代码
server.php
class server{ private $serv; public function __construct() { $this->serv = new swoole_server('0.0.0.0', 9501); $this->serv->set([ 'worker_num' => 2, //开启2个worker进程 'max_request' => 4, //每个worker进程 max_request设置为4次 'task_worker_num' => 4, //开启4个task进程 'dispatch_mode' => 2, //数据包分发策略 - 固定模式 ]); $this->serv->on('start', [$this, 'onstart']); $this->serv->on('connect', [$this, 'onconnect']); $this->serv->on("receive", [$this, 'onreceive']); $this->serv->on("close", [$this, 'onclose']); $this->serv->on("task", [$this, 'ontask']); $this->serv->on("finish", [$this, 'onfinish']); $this->serv->start(); } public function onstart($serv) { echo "#### onstart ####".php_eol; echo "swoole ".swoole_version . " 服务已启动".php_eol; echo "master_pid: {$serv->master_pid}".php_eol; echo "manager_pid: {$serv->manager_pid}".php_eol; echo "########".php_eol.php_eol; } public function onconnect($serv, $fd) { echo "#### onconnect ####".php_eol; echo "客户端:".$fd." 已连接".php_eol; echo "########".php_eol.php_eol; } public function onreceive($serv, $fd, $from_id, $data) { echo "#### onreceive ####".php_eol; echo "worker_pid: {$serv->worker_pid}".php_eol; echo "客户端:{$fd} 发来的email:{$data}".php_eol; $param = [ 'fd' => $fd, 'email' => $data ]; $rs = $serv->task(json_encode($param)); if ($rs === false) { echo "任务分配失败 task ".$rs.php_eol; } else { echo "任务分配成功 task ".$rs.php_eol; } echo "########".php_eol.php_eol; } public function ontask($serv, $task_id, $from_id, $data) { echo "#### ontask ####".php_eol; echo "#{$serv->worker_id} ontask: [pid={$serv->worker_pid}]: task_id={$task_id}".php_eol; //业务代码 for($i = 1 ; $i <= 5 ; $i ++ ) { sleep(2); echo "task {$task_id} 已完成了 {$i}/5 的任务".php_eol; } $data_arr = json_decode($data, true); $serv->send($data_arr['fd'] , 'email:'.$data_arr['email'].',发送成功'); $serv->finish($data); echo "########".php_eol.php_eol; } public function onfinish($serv,$task_id, $data) { echo "#### onfinish ####".php_eol; echo "task {$task_id} 已完成".php_eol; echo "########".php_eol.php_eol; } public function onclose($serv, $fd) { echo "client close.".php_eol; }}$server = new server();
client.php
<?phpclass client{ private $client; public function __construct() { $this->client = new swoole_client(swoole_sock_tcp, swoole_sock_async); $this->client->on('connect', [$this, 'onconnect']); $this->client->on('receive', [$this, 'onreceive']); $this->client->on('close', [$this, 'onclose']); $this->client->on('error', [$this, 'onerror']); } public function connect() { if(!$fp = $this->client->connect("127.0.0.1", 9501 , 1)) { echo "error: {$fp->errmsg}[{$fp->errcode}]".php_eol; return; } } public function onconnect($cli) { fwrite(stdout, "输入email:"); swoole_event_add(stdin, function() { fwrite(stdout, "输入email:"); $msg = trim(fgets(stdin)); $this->send($msg); }); } public function onreceive($cli, $data) { echo php_eol."received: ".$data.php_eol; } public function send($data) { $this->client->send($data); } public function onclose($cli) { echo "client close connection".php_eol; } public function onerror() { }}$client = new client();$client->connect();
以上就是swoole task怎么用的详细内容。