一个人可以对自己或任何其他脚本使用“ http fork”。我的意思是这样的:
// parent sript, called by user request from browser
// create socket for calling child script
$socketToChild = fsockopen("localhost", 80);
// HTTP-packet building; header first
$msgToChild = "POST /sript.php?¶m=value& HTTP/1.0\n";
$msgToChild .= "Host: localhost\n";
$postData = "Any data for child as POST-query";
$msgToChild .= "Content-Length: ".strlen($postData)."\n\n";
// header done, glue with data
$msgToChild .= $postData;
// send packet no oneself www-server - new process will be created to handle our query
fwrite($socketToChild, $msgToChild);
// wait and read answer from child
$data = fread($socketToChild, $dataSize);
// close connection to child
fclose($socketToChild);
...
现在,子脚本:
// parse HTTP-query somewhere and somehow before this point
// "disable partial output" or
// "enable buffering" to give out all at once later
ob_start();
// "say hello" to client (parent script in this case) disconnection
// before child ends - we need not care about it
ignore_user_abort(1);
// we will work forever
set_time_limit(0);
// we need to say something to parent to stop its waiting
// it could be something useful like client ID or just "OK"
...
echo $reply;
// push buffer to parent
ob_flush();
// parent gets our answer and disconnects
// but we can work "in background" :)
...
主要思想是:
用户请求调用的父脚本;
父级在同一台服务器(或任何其他服务器)上调用子级脚本(与父级或另一个)相同,并向它们提供请求数据;
父母对用户说可以并结束;
儿童作品。
如果您需要与孩子互动-可以将DB用作“通信介质”:父母可以读取孩子的状态并写入命令,孩子可以读取命令并写入状态。如果您需要几个子脚本,则应在用户侧保留子ID以区别它们,并在每次要检查各自子状态时将其ID发送给父。
我在这里找到了-http: //linuxportal.ru/forums/index.php/t/22951/