使用PHP在Windows中应用程序线程

时间:2011-05-17 22:30:42

标签: php windows process xampp fork

我正在创建一个PHP应用程序,它生成子进程,其参数详细说明了它们应该执行的工作。更具体地说,子进程将处理来自大型MySQL数据库的行,并且父应用程序将对行进行计数,并生成具有要处理的行数范围的~50个进程。

我需要父进程知道子进程是否完成的方法。之前我已经在Linux中创建了这个应用程序,使用MySQL表来检查子进程。我想这次独立于MySQL进行流程管理(现在我在Windows中)。

PHP父进程是否有办法在子进程创建时获取“句柄”并观察其活动?

我希望我清楚我要做的事情的要点。任何答案和建议将不胜感激。

除了使用其他编程语言的建议之外 - 我正在使用许多PHP库进行处理以及自定义类和函数

3 个答案:

答案 0 :(得分:0)

您可以更轻松地管理。

使用参数运行这些脚本,以便他们使用LIMIT param进行查询,并在总查询结果的“切片”上详细说明您需要的内容

答案 1 :(得分:0)

你无法在Windows中分叉,这是一个简单的事实。 Linux forks,Windows线程。

但是我发现了这个(在Windows中使用php进行线程处理 - 有一些资源):

What's the best way to fork/thread in PHP on Win?

答案 2 :(得分:0)

我曾经构建了一个小样本,用于启动自身的副本以进行一些性能测试。主脚本使用proc_open()来调用子进程。这样,您可以将脚本的输入和输出重定向到主脚本并交换命令/数据。我不再拥有该脚本的来源,但这是一个粗略的大纲:

$fork = array();
$descriptorspec = array(
    0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
    1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
);
for ($i = 0; $i < 50; $i++){
    $fork[$i] = array('process' => NULL, 'pipes' => array());
    $fork[$i]['process'] = proc_open('php script.php', $descriptorspec, $fork[$i]['pipes']);
}
# Loop thru $fork, check feof of the pipes
# write commands, read data
# if all pipes return feof, proc_close($fork[$i]['process'] will return the returncode

我希望这对你有用; )