Research on the implementation of asynchronous operations by PHP
- 2020-05-27 04:35:37
- OfStack
1. Why does PHP need asynchronous operations?
1. Generally speaking, PHP is suitable for short time consuming tasks such as web page display. If it takes time for time-consuming operations such as resize pictures, big data import, batch sending EDM, SMS, etc., it is very easy for operation timeout to occur. You can say I can set an infinite timeout, etc. You should also know that PHP has a working mode fastcgi, PHP infinite no timeout, doesn't mean fastcgi doesn't timeout accordingly... If you also want to say that you want fastcgi to never run over time, I suggest you discuss it with your operations staff to...
This is where the asynchronous operation comes into play, and since it is non-blocking, the operation returns immediately and works slowly in the background. Regardless of whether you timed out or not, I'm not working in the current process/thread. See if it's nice, but it's a hole...
2. Can PHP implement asynchronous operations?
The answer is yes, but the various pure PHP implementations on the web are a bit awkward. socket mode, suspend process mode, and some directly fork process. Very well, each fairy has his own special skill. If the operation and maintenance staff see it, 1 will be............
Is there any better way to implement this asynchronous operation? Yes, now we have to figure out how to turn it on. Check 1 for the main PECL plug-in solution. There is a stack of * * MQ (message queue), and one of them for task assignment comes to our attention, Gearman(actually this guy is the corner, I won't go into details, click the connection to see the introduction).
3. Why choose Gearman?
If nothing else, say that it has a lot of client, client supports many languages, and you can write worker in most of the languages you like. Personally, I'm tired of the language wars. You can write worker in codewords if you like. There is data persistence support (that is, saving queues to the database medium, so failover is fine) and cluster support (many ××MQ have these features). There are extensions on PECL as well as pure PHP implementations. Anyway, this Gearman has lived for a long time, and all the 7 and 8 problems have been basically solved.
4. Basic ideas
The Gearman plug-in is much easier. This is sending a task to gearman, sending the task to be executed, and waiting for worker to call PHP cli to run our php code.
I wrote 1 python worker (don't ask me why I use python, 1. I will python, 2.linux does not need runtime), you can write 1 worker of PHP according to your own thinking, but I am not quite trusted worker of PHP. Try one worker in java, node.js, or another language. If you are interested in writing worker in Golang, please contact me.
phpasync_worker_py
Sorry, there are no comments in it. 1 configuration file, 1 py script. The basic function is to analyze the parameters of the 1 call and then call PHP Cli, and that's it. To get the py script running, install the gearman module of python.
Then go to the section on PHP to test the code:
<?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 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 for debugging, and the rest are all literal. This example is a first impression of how this is called. Then directly on PHP all the source code:
php_async.zip
And then there should be a lot of people saying, gearman doesn't work with win... So let me put java gearman server on it.
java-gearman-service-0.6.6.zip
Conclusion 5.
After the above configuration of rhinoceros 1 big guy (to install 1 Gearman, but also run an Py script), we basically make PHP have asynchronous call function, of course, there is also 1 state maintenance to implement. So it turns out that it's not a very good plan, it's too complicated. It would be better to use 1 web service to do web callback (the problem is that web callback1 runs out of time...) , please pay attention to this later.
In case the above code cannot be downloaded, this site packages the download
The original link: http: / / my oschina. net/wakanoc/blog / 101789