如果要发100封邮件,for循环100遍,这种方法显然是不可取的。
在一些比较繁杂的业务里,我们很可能有超过1万的邮件要群发。那我们怎么处理这个延迟的问题?
答案便是用异步。把“发邮件”这个操作封装,然后后台异步地实行1万遍。这样的话,用户提交网页后,他所等待的韶光只是“把发邮件任务要求推送进行列步队里”的韶光。而我们的后台做事将在用户看不见的地方跑。

在实现“异步队列”这点上,有人采取MySQL表或者redis来存放待发送的邮件,然后,每分钟定时读取待发送列表,然后处理。这便是定时异步任务行列步队。但当条件交的任务要一分钟后才能实行,在某些实时性哀求高的运用处景里还是烦懑,比如发送短信的场景,只要一提交任务,便要立时实行,用户不须要等待返回结果。
以下将磋商用php扩展swoole实现实时异步任务行列步队发送短信的方案。
做事端
第一步:创建tcp做事器
第二步:设置做事器的干系属性
第三步:设置做事真个干系回调函数处理任务
详细代码如下:tcp_server.php
<?phpclass Server{ private $serv; public function __construct(){ $this->serv = new swoole_server(\"大众0.0.0.0\公众,9501); $this->serv->set( array( 'worker_num' => 1, //一样平常设置为做事器CPU数的1-4倍 'daemonize' => 1, //以守护进程实行 'max_request' => 10000, 'dispatch_mode' => 2, 'task_worker_num' => 8, //task进程的数量 \"大众task_ipc_mode \公众 => 3, //利用行列步队通信,并设置为争抢模式 \"大众log_file\"大众 => \公众log/taskqueueu.log\"大众, ) ); $this->serv->on('Receive',array($this,'onReceive')); $this->serv->on('Task',array($this,'onTask')); $this->serv->on('Finish',array($this,'onFinish')); $this->serv->start(); } public function onReceive(swoole_server $serv, $fd, $from_id, $data){ $serv->task($data); } public function onTask($serv, $task_id, $from_id, $data){ $data = json_decode($data,true); if(!empty($data)){ return $this->sendsms($data['mobile'],$data['message']); } } public function onFinish($serv, $task_id, $data){ echo \公众Task {$task_id} finish\n\"大众; } public function sendsms($mobile,$text) { $timestamp = date(\"大众Y-m-d H-i-s\"大众); $pid = \"大众888888888\"大众; $send_sign = md5($pid.$timestamp.\"大众abcdefghijklmnopqrstuvwxyz\公众); $post_data = array(); $post_data['partner_id'] = $pid; $post_data['timestamp'] =$timestamp; $post_data['mobile'] = $mobile; $post_data['message'] = $text; $post_data['sign'] = $send_sign; $url='http://182.92.149.100/sendsms'; $o=\公众\"大众; foreach ($post_data as $k=>$v) { $o.= \公众$k=\"大众.urlencode($v).\"大众&\"大众; } $post_data=substr($o,0,-1); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL,$url); //为了支持cookie //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); if(strpos($result,\"大众success\"大众)!==false) { $outstr=1; } else { $outstr=502; } return $outstr; }}$server = new Server();?>
客户端
启动后端做事后,客户端首先创建tcp客户端做事器,然后连接tcp后端做事器,并向后端tcp做事器发送数据,详细代码如下:client.php
<?phpclass Client{ public $client; public function __construct(){ $this->client= new swoole_client(SWOOLE_SOCK_TCP);//默认同步tcp客户端,添加参数SWOOLE_SOCK_ASYNC为异步 } public function connect(){ if(!$this->client->connect('127.0.0.1',9501,1)){ throw new Exception(sprintf('Swoole Error: %s', $this->client->errCode)); } } public function send($data){ if($this->client->isConnected()){ $data = json_encode($data); //print $data; if($this->client->send($data)){ return 1; }else{ throw new Exception(sprintf('Swoole Error: %s', $this->client->errCode)); } }else{ throw new Exception('Swoole Server does not connected.'); } } public function close(){ $this->client->close(); }}$client= new Client();$client->connect();$data=array( 'mobile'=>'18511487955', 'message'=>'you mobile 18511487955');if($client->send($data)){ echo 'succ';}else{ echo 'fail';}?>
举两个例子,怎么样写好代码
培养自己的PHP代码规范
MySQL必问口试题
轻微高等点的PHP口试题,你敢试吗