资讯

展开

php异步处理

作者:快盘下载 人气:

什么是php异步处理


客户端与服务器端是通过HTTP协议进行连接通讯,客户端发起请求,服务器端接收到请求后执行处理,并返回处理结果。


有时服务器需要执行很耗时的操作,这个操作的结果并不需要返回给客户端。但因为php是同步执行的,所以客户端需要等待服务处理完才可以进行下一步。


因此对于耗时的操作适合异步执行,服务器接收到请求后,处理完客户端需要的数据就返回,再异步在服务器执行耗时的操作。


php异步处理程序案例

a.php
<?phpheader("Content-type: text/html; charset=utf-8");date_default_timezone_set("Asia/Shanghai");
$start = microtime(true);
function fsockopen_get($domain, $url){    $fp = fsockopen($domain, 80, $errno, $errstr, 30);    if (!$fp) {        echo "$errstr ($errno)
";    } else {        $out = "GET " . $url . " / HTTP/1.1
";        $out .= "Host: " . $domain . "
";        $out .= "Connection: Close

";        fwrite($fp, $out);        /*忽略执行结果        while (!feof($fp)) {        echo fgets($fp, 128);        }*/        fclose($fp);    }}fsockopen_get('temp.com', '/b.php');
$end = microtime(true);
$time= $end - $start;
//精确到十位小数,可自行调节echo number_format($time, 10, '.', '')." seconds";
b.php<?phpheader("Content-type: text/html; charset=utf-8");
file_put_contents('54321.log', microtime(true), FILE_APPEND);
sleep(20);
file_put_contents('54321.log', microtime(true), FILE_APPEND);


加载全部内容

相关教程
猜你喜欢
用户评论
快盘暂不提供评论功能!