Research on asynchronous PHP operations. 1. Why does PHP require asynchronous operations? In general, PHP is applicable to web page display tasks that are time-consuming,
1. Why does PHP require asynchronous operations?
In general, PHP is applicable to web page display tasks that are time-consuming. if you want to perform time-consuming operations such as resize images, big data import, batch sending of EDM and SMS, it is easy to see Operation timeout. You can say that I can set an infinite timeout. wait, you also need to know that PHP has a working mode that is fastcgi, and PHP does not time out infinitely. it does not mean that fastcgi does not time out ...... If you want to say fastcgi never times out, I suggest you discuss it with your O & M personnel ......
At this time, the asynchronous operation will play its role. because of the non-blocking operation, the operation will return immediately and then work slowly in the background. If you do not time out, I will not work in the current process/thread. Check whether it is beautiful, but it is actually a pitfall ......
2. can PHP implement asynchronous operations?
The answer is yes, but the pure PHP implementation on the Internet is a bit awkward. Socket mode, pending process mode, and some directly fork processes. Good. all the gods see each other. If the O & M personnel see this, they will certainly be xxx yours. it's strange not to run the web server to death ......
Is there any other better way to implement this asynchronous operation? Yes. now we only want to open plug-ins. Check that there are a bunch of ×× MQ (Message Queue) plug-ins in main PECL plug-ins. one plug-in used for task allocation comes into our sight, Gearman (in fact, this guy is the horn, I will not introduce it in detail. Click Connect to see the introduction ).
3. Why choose Gearman?
If you don't talk about anything else, it means that there are many clients that support many languages. you can use most of your favorite languages to write worker. Personally, I am very tired of language competition. you like to write worker in the CAPTCHA language as you like. Support for data persistence (that is, saving the queue to the database medium, and fault recovery) and cluster support (in fact many ×× MQ have these features ). PECL has extensions and pure PHP extensions. This Gearman has been active for a long time, and the problem is basically solved.
4. basic ideas
With Gearman, this plug-in is much simpler. It is to send a task to gearman, send the executed task, and then wait for worker to call PHP cli to run our php code.
I wrote a python worker (don't ask why I use python, 1. I will use python, 2. in linux, you don't need to install runtime. you can write a PHP worker based on your own ideas. However, I don't really trust the worker that PHP runs. You can use java, node. js, or other languages to implement a worker. If you are interested in using Golang to write worker, contact me.
Phpasync_worker_py
Sorry, there is no comment in it. A configuration file and a py script. The basic function is to analyze the called parameters and then call PHP Cli. To run the py script, install the python gearman module.
Then test the code in the PHP section:
The code is as follows:
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 callback ');
PHPAsyncClient: parse ();
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' => array (
'Content' => 'Hello Callback! ',
),
), _ FILE __);
Return;
}
}
Static public function callback ($ args ){
Self: log ('asynctest callback run ');
Self: log ('asynctest callback args: '. print_r ($ args, true ));
}
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 three static methods, one is the log method used for debugging, and the other is literal. This example gives a preliminary impression on this call method. Then directly go to all the PHP source code:
Php_async.zip
Then many people will say that gearman cannot be installed in win ...... So I will also release the gearman server for java.
Java-gearman-service-0.6.6.zip
5. Conclusion
After the above configuration of a guy with the same size as the rhinoceros (to install a Gearman and run a Py script), we basically made PHP have the Asynchronous call function, of course, there is also a state maintenance shenma to achieve it on its own. So we found that this solution is not so complicated. It would be better to use some web service methods to do web callback (the problem is that web callback times out ......), Please pay attention to the future.
To prevent the above code from being downloaded, we recommend that you package and download it.
Link: http://my.oschina.net/wakanoc/blog/101789
Why? In general, PHP is applicable to web page display tasks that are time-consuming ,...