<?php $server = new Swoole\Server('127.0.0.1',9501); $server->set( [ 'worker_num'=>2,//worker进程数 'task_worker_num'=>2,//Task worker 进程数 ] ); $server->on('receive',function( $server,$fd,$reactorId,$data){ var_dump($data); $data = json_decode($data,true); if($data['action'] == "register"){ echo "接收到注册请求:{$data['email']}\n"; $taskData = [ 'action'=>'send_email', 'email'=>$data['email'], 'subject'=>'欢迎注册', 'body'=>"感谢您注册我们的网站" ] ; $server->task($taskData); $server->send($fd,"注册成功"); }; }); //处理异步任务 $server->on('task',function( $server,$taskId,$srcWorkerId,$taskData){ if($taskData['action'] == "send_email"){ echo "正在发送邮件到:{$taskData['email']}\n"; sleep(3); echo "邮件发送成功:{$taskData['email']}\n"; } $server->finish("=任务{$taskId}完成="); }); $server->on('finish',function ( $server,$taskId,$data){ echo "任务完成回调{$data}\n"; }); $server->start();
标签:echo,Task,swoole,示例,worker,server,taskData,data,email From: https://www.cnblogs.com/aln0825/p/18670511