分享

php进程后台调用(多线程/进程)

 Ralf_Jones 2007-05-29

php进程后台调用

这两天在研究php模拟多线程的问题,碰到一个问题就是无论exec、popen、还是proc_open都会造成等待,也就是阻塞式的调用,而我想要得是无阻塞的调用,让程序在后台执行就可以解决问题,搜索之后找到了解决办法:

<?php
/* Note that the call itself isn‘t sanitized in any way, so it‘s
important to make sure that this can‘t be exploited, see
docs for escapeshellcmd() for details
*/
// Demonstration
if(launchBackgroundProcess(‘touch testfile.txt‘)){
print ‘Successfully launched background process‘;
}
/**
* Launch Background Process
*
* Launches a background process (note, provides no security itself, $call must be sanitized prior to use)
* @param string $call the system call to make
* @author raccettura
*/
function launchBackgroundProcess($call) {
// Windows
if(is_windows()){
pclose(popen(‘start /b ‘.$call.‘‘, ‘r‘));
}
// Some sort of UNIX
else {
pclose(popen($call.‘ /dev/null &‘, ‘r‘));
}
return true;
}
/**
* Is Windows
*
* Tells if we are running on Windows Platform
* @author raccettura
*/
function is_windows(){
if(PHP_OS == ‘WINNT‘ || PHP_OS == ‘WIN32‘){
return true;
}
return false;
}
?>

关键在于

‘ /dev/null &‘

(*nix下的后台运行方式)

‘start /b ‘

(windows下的后台运行方式)

想了一下,似乎很多脚本都可以用这种方式将一些操作移交后台进行,可以大幅提高效率。

如果要返回结果,则可以用异步执行并返回状态的方式,边处理边显示处理结果,这可以用在一些耗时较长的操作上,比如论坛备份\恢复等:

<?php
/****
* Again notice this is unsanitized since we trust ourselves.  Coming from the web
* it would need to be sanitized to ensure it‘s safe to use.  see escapeshellarg()
****/
$hostname = ‘accettura.com‘;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www./TR/xhtml11/DTD/xhtml11.dtd">
<HTML xmlns="http://www./1999/xhtml">
<head>
<title>Traceroute to $hostname</title>
</head>
<body>
<?php
if(is_windows()){
$cmd = ‘tracert -w 10‘;
} else {
$cmd = ‘traceroute -w 10‘;
}
$handle = popen("$cmd $hostname 2>&1", ‘r‘);
while(!feof($handle)) {
$buffer = fgets($handle);
$buffer = ‘<p>‘.trim(htmlspecialchars($buffer)).‘</p>‘;
echo $buffer;
ob_flush();
flush();
}
pclose($handle);
/**
* Is Windows
*
* Tells if we are running on Windows Platform
* @author raccettura
*/
function is_windows(){
if(PHP_OS == ‘WINNT‘ || PHP_OS == ‘WIN32‘){
return true;
}
return false;
}
?>
</body>
</html>
 

继续php多线程/进程的问题

昨天找到了进程后台运行的方法,今天测试了一下,发现popen的速度很慢,要40-50毫秒,exec更慢!类似的程序调用命令,都要经过系统调用,每次都开启一个php进程想必很慢。

比较笨的办法还是用fsockopen去通过http在server端get,试了一下,这样不会慢,缺点是增加了apache负载,每个请求都要在后台再请求一次。

我写了段脚本test.php,用fsockopen循环连接本地另外一个脚本test1.php,不做任何操作立即关闭连接,test1.php每次在文本文件test.cache中写入一行,循环100次的时候执行很快,test.cache中也正确的记录了100行。当循环1000次的时候,问题就来了,test.php执行了21.6888360977秒,也就是21秒内向apache发了1000个请求连test1.php,系统马上没有响应了,内存占用飙升到1G多,一分钟之后才恢复正常。test.cache中丢失63行,可能是由于apache超载造成的,但是系统内存却始终没有降下来,apache的占用了83M,剩下的不知道怎么回事- -
最后又找了半天,找到了fork实现的真正多线程!fork是pcntl(Process Control Functions)下的一个函数,pcntl只支持*nix系统,目前没有windows下的相关模块。(文档中说需要在编译php时--enable-pcntl,我用phpize编译成php模块的方式,通过在php.ini中添加extension=pcntl.so也可以使用。)


php手册里面就有了,但是在网上几乎找不到中文的文档!文档里面有这样一个实例:

<?php
declare(ticks=1);
echo "I‘m going to be a Dad.n";
if (spawn_child(‘child_function‘)) {
echo ‘Parent pid:‘.posix_getpid()."n";
}
echo "I‘m going to be a Dad again!.n";
if (spawn_child(‘child_function‘,1,2,3,4)) {
echo ‘Child - 2 Parent pid:‘.posix_getpid()."n";
}
echo "What you‘re pregnant again!?.n";
if (spawn_child(‘grand_children‘)) {
echo ‘Child - 3 Parent pid:‘.posix_getpid()."n";
}
function grand_children() {
echo "Dad I‘m going to have a baby.n";
spawn_child(‘child_function‘,‘Joe‘);
echo "I‘m so proud of my kids.n";
}
function spawn_child($function) {
$original_pid = posix_getpid();
$pid = pcntl_fork();
if ($pid == -1) {
die ("Unable to spawn childn");
}
$new_pid = posix_getpid();
if ($new_pid == $original_pid) {
return true;
}
if (function_exists($function)) {
$numargs = func_num_args();
if ($numargs > 1) {
$args = func_get_args();
//print_r ($args);
unset($args[0]);
call_user_func($function,$args);
} else {
call_user_func($function);
}
echo "Done with child ".$new_pid." they moved out.n";
exit;
} else {
die ("$function does not existn");
}
}
function child_function() {
echo ‘Child pid:‘.posix_getpid()."n";
$args = func_get_args();
if (!empty($args))
print_r ($args);
}
pcntl_wait( $status);
?>

用pcntl实现的多线程可以解决很多问题了,但是似乎还是不能解决我的问题。在多线程虽然可以让程序并行执行,但所有的程序仍然在前台完成,在所有的线程完成之前,浏览器仍然会显示载入状态。在这种状态下,javascript代码不会执行,而我正是需要php产生一段javascript脚本,我想我的目的可能不是多线程或者并发处理,而是异步调用、后台执行,让生成脚本之外的操作在后台执行。
所以我的这种情况用最开始的fsockopen()方法可能更有效,但是fsockopen是不能进行异步处理的,如果要跟打开的连接进行交互,就没有任何优势可言了。不过我在网上找到了可以进行异步通信的办法,其实是变相的实现了多线程:php多路复用(多线程)[翻译]

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多