PHP for asynchronous task queues uses Swoole to implement a real-time asynchronous task queue.

Source: Internet
Author: User
Tags php server
Background: About PHP asynchronous callback what, in fact, PHP this language operating mechanism to gracefully implement is impossible, there is a call swoole extension implementation of this queue processing is very good, before the Zhang Yi brother wrote a queue called HTTPS processing (based on libevent), Behind the Hantian brothers wrote the Swoole, more attention to the asynchronous IO implementation of the CPU IO to eat full (based on their own written epoll plus queue list memory allocation a bunch of things, anyway, I have seen no understanding of the time to study again), but as a Swoole fan and consultant, Asynchronous problem in the PHP industry is still very large demand, especially the log asynchronous write, URL access, mail asynchronous, inter-room DB Special small synchronization, audit asynchronous queue, the framework of the bottom of the interface to troubleshoot the error DB and cache interface query and return, These are currently for large concurrent sites need to be asynchronous to solve, but asynchronous to asynchronous, callback this block also implement asynchronous callback (really big concurrency want to know that the results may block the PHP process generation process waiting for asynchronous return and the new connection can not be processed in a timely manner PHP-FPM process avalanche), In the actual use of the conventional approach is to open a new port, the following swoole is also open a new port to deal with, in HTTPSQS different is swoole can be simple curl what logic can also be encapsulated inside, in Httpsqs just pure queue, a delivery in, A PHP daemon to read the queue, because if it is really busy, resulting in asynchronous processing return slow, this wait is too long, I think to see the specific business and see not to wait, in the actual asynchronous and the queue is mostly used to throw data and decoupling, swoole in this is not only asynchronous and asynchronous callback, Therefore, to maximize the resolution of the above scenarios, first copy an article to say, there is no time to study its implementation, if you can read the words:-), asynchronous callback this block if you have a clear message to me, thank you.

————————————————————————————————————————————————————————————————————————

About asynchronous task queues

The user has opened our website. All he has to do is tick the list of agents that need to send the email and send out the billing email.

If we need to send 1 e-mails, we write a function to execute. Consider that the network may be a little bit delayed, but it is acceptable, the user will be waiting for your page after the mail and then close the page.

If we are going to post 10 messages, use a For loop and loop 10 times to perform the outgoing mail operation. At this time, perhaps 10 times times the network delay will make the user a little bit impatient, but reluctantly can wait.

If you want to send 100 e-mails, for loop 100 times, the user directly up, what broken website!

But in fact, we are likely to have more than 10,000 messages. How to deal with this delay problem?

The answer is to use async. The "Send Mail" operation is encapsulated and then executed asynchronously 10,000 times in the background. In this case, when the user submits the page, the time he waits is just the time to "push the email task request into the queue." And our backend service will run where the user can't see.

In implementing the asynchronous queue, someone uses a MySQL table or Redis to hold the messages to be sent, and then reads the list to be sent every minute and then processes it. This is the timed asynchronous task queue. However, the currently committed task will take a minute to execute, and in some real-time scenarios, it is still unpleasant. Some scenarios require that only one commit task executes immediately, but the user does not need to wait for the result to be returned.

On the cloud platform SAE and BAE, there are taskqueue services to solve the above problems. And if you're assuming a server, how do you fix it? This article explores the implementation of a real-time asynchronous task queue with PHP extension swoole.

Installing Swoole

PECL Installation:

PECL Install Swoole

Look at the command prompt, and if it prompts you to say no to write php.ini, then manually add it to the php.ini after you:

Extension = "swoole.so"

Service side

In the directory where you intend to place the script (you can also create your own), create a new server.php with the following code:

 serv = new Swoole_server ("0.0.0.0", 9501); $this->serv->set (The array (' worker_num ' = 1,//is typically set to 1-4 times the number of server CPUs ' daemonize ' = 1,//To keep Protect process Execution ' max_request ' = 10000, ' dispatch_mode ' = 2, ' task_worker_num ' + 8,//t The number of ask processes "Task_ipc_mode" = 3,//Use Message Queuing communication, and set to scramble 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 ']) {return $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 URL curl_setopt ($CURLOBJ, Curlopt_returntransfer, 1);        Returns the result of the curl_exec to curl_setopt ($CURLOBJ, Curlopt_ssl_verifypeer, FALSE);           curl_setopt ($CURLOBJ, Curlopt_ssl_verifyhost, FALSE);         curl_setopt ($CURLOBJ, Curlopt_header, 0);   Whether to output return header information $response = curl_exec ($CURLOBJ);          Executive Curl_close ($CURLOBJ);    Close the session return $response; }} $server = new server ();

Because the server is asynchronous, resident memory, it must be started from the command line. Execute the above code at the command line to start the service

PHP server.php

Close the command-line window when you are finished executing. The service runs in the background as a daemon

Client

After starting the service, let's look at how to invoke the service. New 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 above code, the URL is the address of the task, and Param is the desired pass-through parameter.

The asynchronous task queue is implemented by saving code, executing client_test.php in the command line or in the browser. The URL that you fill in will be executed asynchronously after each asynchronous task is committed, in HTTP GET.

View and Close

Swoole seems to have no convenient way to close. So it can only be closed directly by closing the process.

To view a command:

Ps-ef | grep php

To end a single process:

kill-9 {Process Number}

command to end all processes:

killall-9 PHP

From the Seventh Stardust blog: http://blog.star7th.com/2016/01/1905.html

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.