Research on asynchronous operation in PHP

Source: Internet
Author: User
Tags array implement log php cli php code return zip client

1. Why does PHP need asynchronous operation?

Generally speaking, PHP is suitable for the occasion of Web page display, such as time-consuming and relatively short tasks, if for more time to spend the operation such as resize pictures, large data import, bulk send EDM, SMS, etc., it is very easy to operate overtime. You can say I can set unlimited timeout time, and so on you also need to know that PHP has a working mode is fastcgi,php Infinite does not time out, does not mean that the fastcgi corresponding no timeout ... If you still want to say that fastcgi should never time out, I suggest you discuss it with your transport personnel ...

This time the asynchronous operation to play his role, because the non-blocking operation, the operation will immediately return, and then work slowly in the background. If you don't time out, I don't work under the current process/thread. See if it's beautiful, but it's also a pit ...

2.PHP can I implement asynchronous operation?

The answer is yes, but all kinds of pure PHP on the web is a bit awkward to implement. Socket mode, suspend process mode, some also directly fork process. Very good, each road immortal recount. If the operators see it, will be xxxxx you, do not run the Web server to death is strange ...

Is there any better way to implement the possibility of this asynchronous operation? Now we just have to figure out how to hang out. Check the PECL mainstream plug-in scheme has a bunch of xxmq (Message queue), which has a task assigned to the plug-in into our line of sight Gearman.

3. Why Choose Gearman?

If you don't say anything else, say that he has more client and support a client in many languages, and you can use most of your favorite language to write about worker. I am very annoyed with the language, you like to write in God code language worker is as you like. There is data persistence support (which is to save the queue to the database media, that failback is done), with cluster support (in fact, many xxmq have these features). There are extensions on the pecl, and there is a pure PHP implementation extension. Anyway this gearman also lived for a long time, assorted problem is basically solved.

4. Basic Ideas

With the Gearman this plug-in is much simpler. is to send a task to Gearman, issue the execution of the task, and wait for the worker to invoke the PHP CLI to run our PHP code.

I wrote a python worker (don't ask me why I used python,1. I will python,2.linux under the runtime), you can write a PHP worker by yourself, but well, I am not very trustworthy in PHP run worker. Other language meals can be used in Java, node.js or other languages to achieve a worker try. Friends who are interested in using Golang to write the worker can find me.

Download: phpasync_worker_py

Sorry, there is no annotation inside. A configuration file, a PY script. The basic function is to analyze the parameters of the call, and then invoke the PHP Cli, just like that. To get the py script running, install the Python gearman module yourself.

Then go to the PHP section and test the code first:

<?php require_once ' phpasyncclient.php ';

Date_default_timezone_set (' Asia/shanghai ');

    Class Asynctest {Const Log_file = '/debug.log '; static public Function run () {if (Phpasyncclient::in_callback (__file__)) {self::log (' php Async callb
            Ack ');
            Phpasyncclient::p arse ();
        Return
            } if (Phpasyncclient::is_main (__file__)) {Self::log (' main run ');
            $async _call = Phpasyncclient::getinstance ();  $async _call->asynccall (' asynctest ', ' callback ', Array (' content ' => ' Hello world!!! '), Array (' class ' => ' asynctest ', ' method ' => ' callback ', ' params ' =&G T
            Array (' content ' => ' Hello callback! ',),), __file__);
        Return
        The static public function callback ($args) {self::log (' Asynctest callback Run '); Self: Log (' Asynctest callback args: '. Print_r ($args, true));
        The static public function log ($content) {$fullname = DirName (__file__). Self::log_file; $content = Date (' [y-m-d h:i:s] '). $content. "
        \ n ";
    File_put_contents ($fullname, $content, file_append); } asynctest::run ();?>

There are 3 static methods, one is the log method for debugging, and the other is literal meaning. This example is an initial impression of the way it is invoked. And then directly on all of PHP's source code:

Download: Php_async.zip

Then there should be a lot of people will say, win under the installation can not Gearman ... So I put the Java version of Gearman server on it.

Download: Java-gearman-service-0.6.6.zip

5. Conclusion

After a guy with the same size as the rhino (to install a gearman and run a py script), we basically make PHP have the asynchronous invocation function, of course, there is a state to maintain the horse's own to achieve.



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.