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


Application threading in Windows using PHP

我正在制作一个PHP应用程序,该应用程序生成子进程,参数详细说明了它们应该做什么工作。更具体地说,子进程将处理来自大型 MySQL 数据库的行,父应用程序将计算行并生成 ~50 个进程,其中包含要处理的行跨度。

我需要一种方法让父进程知道子进程是否已完成。我以前在 Linux 中制作过这个应用程序,使用 MySQL 表供子进程签入。这次我想独立于MySQL进行进程管理(现在我在Windows中)。

有没有办法让 PHP 父进程在创建时获取子进程的"句柄"并观察它的活动?

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

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

我曾经构建过一个小样本,它启动了自己的副本以进行一些性能测试。主脚本使用 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

我希望其中一些对你有用;)

您可以更轻松地管理它。

使用

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

你不能在Windows中分叉,这是简单的事实。Linux forks,Windows threads。

但是我发现了这个(在窗口中使用 php 线程 - 有一些资源):

在 PHP 中分叉/线程的最佳方法是什么?