php 异步操作

PHP异步执行的常用方式:

exec

这是最简单的方式

<?php

exec(sprintf("%s > %s 2>&1 & echo $! > %s", $cmd, $outputfile, $pidfile));

调用$cmd命令,将输出重定向到$outputfile,不显示错误信息,同时将进程id输出到$pidfile

这样也方便监控,比如判断该进程是否还在运行

<?php

function isRunning($pid){

    try{

        $result = shell_exec(sprintf("ps %d", $pid));

        if( count(preg_split("/\n/", $result)) > 2){

            return true;

        }

    }catch(Exception $e){}

 

    return false;

}

注意:如果直接调用exec来运行某个命令,或者在该命令后面加个"&"php还是会等待该命令运行完成再执行下面的操作。

proc_open/proc_close

这个方法很有意思,先用proc_open运行一段后台程序,然后用proc_close来关闭proc_open,结果程序就在后台运行了,同时php也会继续执行下去

<?php

proc_close(proc_open ("ping www.baidu.com -c 10 > /path/to/output &", array(), $foo));

pcntl_fork

使用php的多线程来达到目的,原理就是复制一个子线程,同时杀死父线程(不支持windows)

<?php

if ($pid = pcntl_fork())

    die();     // Parent

 

function shutdown() {

    posix_kill(posix_getpid(), SIGHUP);

}

 

if(ob_get_level()) ob_end_clean(); // Discard the output buffer and close

 

fclose(STDIN);  // Close all of the standard

fclose(STDOUT); // file descriptors as we

fclose(STDERR); // are running as a daemon.

 

register_shutdown_function('shutdown');

 

if (posix_setsid() < 0)

    die();      // <- This is an error

 

// Do your stuff here

exec('/bash/command > /path/to/output');

header

使用这个方法的前提是使用http协议,同时目标文件可控,最好在一个域下。因为必须要建立http连接才行,所以稍微有点费时。原理是通过header输出’Connection:close’头,中断http连接,同时后面的代码继续执行。

<?php

while(ob_get_level()) ob_end_clean();

header('Connection: close');

ignore_user_abort();

ob_start();

echo('Connection Closed');

$size = ob_get_length();

header("Content-Length: $size");

ob_end_flush();

flush();

使用数据库作中转

把要执行的命令和参数先存到缓存或数据库,接下来的事就不用php操心了。




1.客户端页面采用AJAX技术请求服务器

1. 最简单的办法,就是在返回给客户端的HTML代码中,嵌入AJAX调用,或者,嵌入一个img标签,src指向要执行的耗时脚本。
这种方法最简单,也最快。服务器端不用做任何的调用。
但是缺点是,一般来说Ajax都应该在onLoad以后触发,也就是说,用户点开页面后,就关闭,那就不会触发我们的后台脚本了。
而使用img标签的话,这种方式不能称为严格意义上的异步执行。用户浏览器会长时间等待php脚本的执行完成,也就是用户浏览器的状态栏一直显示还在load。
当然,还可以使用其他的类似原理的方法,比如script标签等等


2.popen()函数

resource popen ( string command, string mode );
//打开一个指向进程的管道,该进程由派生给定的 command 命令执行而产生。打开一个指向进程的管道,该进程由派生给定的 command 命令执行而产生。

所以可以通过调用它,但忽略它的输出。

?
pclose(popen( "/home/xinchen/backend.php &" , 'r' ));

  这个方法避免了第一个方法的缺点,并且也很快。但是问题是,这种方法不能通过HTTP协议请求另外的一个WebService,只能执行本地的脚本文件。并且只能单向打开,无法穿大量参数给被调用脚本。

并且如果,访问量很高的时候,会产生大量的进程。如果使用到了外部资源,还要自己考虑竞争。


3.CURL扩展

CURL是一个强大的HTTP命令行工具,可以模拟POST/GET等HTTP请求,然后得到和提取数据,显示在"标准输出"(stdout)上面

$ch = curl_init();

$curl_opt = array(CURLOPT_URL, 'http://www.example.com/backend.php',
CURLOPT_RETURNTRANSFER, 1,
CURLOPT_TIMEOUT, 1,);

curl_setopt_array($ch, $curl_opt);

curl_exec($ch);

curl_close($ch);
复制代码

使用CURL需要设置CUROPT_TIMEOUT为1(最小为1,郁闷)。也就是说,客户端至少必须等待1秒钟。

4.fscokopen()函数

fsockopen是一个非常强大的函数,支持socket编程,可以使用fsockopen实现邮件发送等socket程序等等,使用fcockopen需要自己手动拼接出header部分



request.php

<?php
/**
 * php异步请求
 * $args array[
 *   "host":主机
 *   "url":地址
 *   "method":方法
 *   "data":数据
 * ]
 */
function asyn_request($args) {
    $host = $args["host"] ?  $args["host"] : "localhost";//主机
    $method = $args["method"] == "POST" ? "POST" : "GET";//方法   
    $url = $args["url"] ? $args["url"] : "http://".$host ;//地址
    $data = is_array($args["data"]) ? $args["data"] : array();//请求参数   
    $fp = @fsockopen($host,80,$errno,$errstr,30);
    //错误
    if(!$fp){echo"$errstr ($errno)<br/>\n";exit;}
   
    $qstr = http_build_query($data);//请求参数
   
    $params.= $method == "GET" ? "GET {$url}?{$qstr} HTTP/1.1\r\n" :  "POST {$url} HTTP/1.1\r\n";
    $params.= "Host: ".$host."\r\n";
    $params.= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5\r\n";
    $params.= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
    $params.= "Accept-Language: zh-cn,zh;q=0.5\r\n";
    $params.= "Accept-Encoding: gzip,deflate\r\n";
    $params.= "Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n";
    $params.= "Keep-Alive: 300\r\n";
    $params.= "Connection: keep-alive\r\n";
    $params.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $params.= "Content-Length: ".strlen($qstr)."\r\n\r\n";
    $params.= $method == "GET" ? null :$qstr;
   
    //file_put_contents("C:\\http.txt",$params);
   
    fwrite($fp, $params);
    fclose($fp);
}


$args["host"] = "localhost";//主机
$args["url"] = "http://localhost/test/socket/doing.php";//异步执行的脚本
$args["method"] = "POST";//请求方式
$args["data"] = array("a"=>"中","b"=>"国");//参数


asyn_request($args);

?>


doing.php

<?php
   file_put_contents("C:\\log.txt",serialize($_POST));
?>

http.txt

POST http://localhost/test/socket/doing.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 29

a=%E4%B8%AD&b=%E5%9B%BD222222


log.txt
a:2:{s:1:"a";s:3:"中";s:1:"b";s:3:"国";}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值