Home  >  Article  >  Backend Development  >  How to implement asynchronous processing in php

How to implement asynchronous processing in php

小云云
小云云Original
2018-03-21 11:17:292308browse

In the actual production environment, it is very common for PHP to be used as a backend interface server. PHP certainly has the advantages of being a backend server, but it shows up when processing some results that the client does not care about. The disadvantage is that there is no asynchronous execution mechanism. For example, when we want to record the performance of a certain client's access to PHP (including the start time, end time, result status, etc.), the client of course wants to be able to respond to PHP's processing as soon as possible. to get the results, and if you install a conventional solution, the client will have to wait for PHP to complete the performance recording before getting the results. It's equivalent to going to the bank to check your current balance, and the teller runs over and chats with other people for a while, but the result is the same when he comes to tell you.

So, many times, you need a php that can perform asynchronous operations.

How can we achieve asynchronous implementation?

One of the solutions is to use PHP system calls to start a new process to achieve this.

php provides the fsockopen function. The function of this function is to initialize a socket connection to the specified host. By default, the socket connection will be opened in blocking mode. Of course you can convert it to non-blocking mode through stream_set_blocking(). This is the key. Therefore, the idea is: open a non-blocking socket to connect to the local machine, and the local machine will do some time-consuming processing after receiving it.

A processing code similar to this (file posttest.php):

$fp = fsockopen($php_Path,80);
if (!$fp) {
    LMLog::error("fsockopen:err" );
} else {
    $out = "GET /album/action/album_write_friends_thread_record.php?key=&u=   HTTP/1.1\r\n";
    $out .= "Host: ".$php_Path."\r\n";
    $out .= "Connection: Close\r\n\r\n";
    stream_set_blocking($fp,true);
    stream_set_timeout($fp,1);
    fwrite($fp, $out);
    usleep(1000);
    fclose($fp);
}

Here, usleep(1000) is very critical, it can ensure that this request can be sent out.

We are looking at the processing code logic (file album_write_friends_thread_record.php):

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-09-23
 * Time: 09:26
 */
/**
 * 客户端调用服务器接口页面
 * user: guwen
 */
sleep(20);// 睡眠20s
?>实际上,我们服务器在执行fsockopen 那段程序时,就不会再等20s之后才能返回给客户端,而是发出这个请求之后,即返回客户端,销毁进程,
而把剩余的工作交由其他进程慢慢做去,这就实现了php的异步。

Related recommendations:

PHP asynchronous processing Implementation plan

The above is the detailed content of How to implement asynchronous processing in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn