For asynchronous task queue, PHP uses swoole to implement real-time asynchronous task queue. Background: What about PHP asynchronous callback? in fact, it is impossible for the operating mechanism of PHP to be elegantly implemented. it is very good to have a swoole extension to process this queue, A banquet brother previously wrote a queue processing service called https (based on libevent), followed by swoole written by Han Tianfeng, I pay more attention to asynchronous I/O implementation to fully utilize cpu I/O (based on the self-compiled epoll and the queue linked list memory to allocate a bunch of things, I did not see it before I study it ), however, as a fan and consultant of swoole, there is still a huge demand for asynchronous issues in the PHP industry, in particular, asynchronous log writing, url access, asynchronous mail sending, small synchronization across data centers, audit asynchronous queues, and db and cache interface query and return of interface access troubleshooting errors at the underlying framework, currently, these solutions require asynchronous processing for highly concurrent websites, but asynchronous processing is asynchronous, the callback module also implements asynchronous callback (the result may be congested by the PHP process and the process is waiting for asynchronous return while the new connection cannot process the php-fpm process avalanche in time ), in practical use The solution is to open a new port, and the swoole below also opens a new port for processing. Unlike httpsqs, swoole can encapsulate the logic of simple curl, in httpsqs, it is only a pure queue, one is delivered in, and another php daemon is started to read the queue, because if it is really busy, resulting in slow asynchronous processing and return, this wait is too long, I think it depends on the specific business. In reality, asynchronization and queuing are mostly used for data throwing and decoupling. swoole not only has asynchronization but also asynchronous callback, so, the above scenario has been solved to the maximum extent. I will first copy an article and then study its implementation when I have time. if I can understand it :-), if you have any idea about asynchronous callback, leave a message to me. thank you.
------------------------------------------------------------------------
About asynchronous task queue
The user opened our website. All he needs to do is to check the list of agents that need to send emails, and then send the settlement email.
If we need to send one email, we can write a function to execute it. Considering that the network may be a little delayed, but acceptable, the user will wait for your webpage to finish sending emails and then close the webpage.
If we want to publish 10 emails, use a for loop and execute the mail operation 10 times in a loop. At this time, perhaps 10 times the network latency will make users a little impatient, but barely wait.
For example, if you want to send 100 emails, and for 100 times, the user can directly crack the website!
But in fact, we are likely to have more than 10 thousand emails. How to deal with this latency problem?
The answer is asynchronous. Encapsulate the "send email" operation, and then execute it 10 thousand times asynchronously in the background. In this case, after a user submits a webpage, the user waits only for the time to "push the email task request to the queue. Our background services will run in places invisible to users.
In the implementation of "asynchronous queue", some people use mysql tables or redis to store emails to be sent, and then regularly read the list to be sent every minute, and then process it. This is the scheduled asynchronous task queue. However, the current submitted task can only be executed one minute later, which is still not satisfactory in some real-time requirements. In some scenarios, you only need to submit the task and execute it immediately, but you do not need to wait for the returned result.
On the cloud platform SAE and BAE, there are taskqueue services to solve the above problems. How can we solve this problem if we assume that the server is used? This article will discuss how to use php to extend swoole to implement real-time asynchronous task queue.
Install swoole
Install pecl:
Pecl install swoole
Check the command line prompt. if it prompts that php. ini is not written, add the following manually after PHP. ini:
Extension = "swoole. so"
Server
Create Server. php in the directory where you want to place the script (you can also create it yourself). The code is as follows:
Serv = new swoole_server ("0.0.0.0", 9501); $ this-> serv-> set (array ('worker _ num' => 1, // generally set to 1-4 times the number of server CPUs 'daemonize '=> 1, // run 'max _ request' => 10000 with the daemon process, 'Dispatch _ mode' => 2, 'task _ worker_num '=> 8, // number of task processes "task_ipc_mode" => 3, // use message queue for communication, and set it to the competition mode // "log_file" => "log/taskqueueu. log ", // log); $ this-> serv-> on ('receive ', array ($ this, 'onreceive ')); // bind callback $ 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) {// echo "Get Message From Client {$ fd }:{ $ data} n"; // send a task to task worker. $ serv-> task ($ data);} public function onTask ($ serv, $ task_id, $ from_id, $ data) {$ array = json_decode ($ data, true ); if ($ array ['URL']) {ret Urn $ this-> httpGet ($ array ['URL'], $ array ['param']);} public function onFinish ($ serv, $ task_id, $ data) {// echo "Task {$ task_id} finishn"; // echo "Result: {$ data} n" ;}protected function httpGet ($ url, $ data) {if ($ data) {$ url. = '? '. Http_build_query ($ data) ;}$ curlObj = curl_init (); // initialize curl, curl_setopt ($ curlObj, CURLOPT_URL, $ url); // Set the url curl_setopt ($ curlObj, fail, 1); // return curl_exec results to curl_setopt ($ curlObj, fail, FALSE); curl_setopt ($ curlObj, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt ($ curlObj, CURLOPT_HEADER, 0); // whether to output the returned header information $ response = curl_exec ($ curlObj); // execute curl_close ($ curlObj); // close the session return $ response ;}} $ server = new Server ();
Because the server is asynchronous and resident memory, it must be started through the command line. Execute the above code on the command line to start the service
Php Server. php
Close the command line window after execution. The service runs in the background as a daemon.
Client
After starting the service, let's see how to call the service. Create a test file Client_test.php
The code is as follows:
client = new swoole_client(SWOOLE_SOCK_TCP); } public function connect() { if( !$this->client->connect("127.0.0.1", 9501 , 1) ) { echo "Connect Error"; } $data = array( "url" => "http://192.168.10.19/send_mail" , "param" => array( "username"=>'test', "password" => 'test' ) ); $json_data = json_encode($data); $this->client->send( $json_data ); }}$client = new Client();$client->connect();
In the code above, the url is the address of the task, and param is the required parameter.
Save the code and execute Client_test.php in the command line or browser to implement asynchronous task queue. The URL you entered will be asynchronously executed in http get mode after each asynchronous task is submitted.
View and close
Swoole does not seem to have a convenient way to close it. Therefore, you can only close the process directly.
View the command:
Ps-ef | grep php
End a single process:
Kill-9 {process number}
Command to end all processes:
Killall-9 php
From Stardust 7 Blog: http://blog.star7th.com/2016/01/1905.html