php 一些分叉进程继续运行
我有一个 php 脚本,它将一个任务分为多个部分,并在单独的子进程中运行每个部分。代码如下所示:
foreach($users as $k => $arr) {
if(($pid = pcntl_fork()) === -1) continue;
if($pid) {
pcntl_wait($status,WNOHANG);
continue;
}
ob_start();
posix_setsid();
dbConnect();
// do stuff to data
exit();
}
我在 Debian 服务器上使用 crontab 运行此脚本,但问题是某些进程继续运行并保留内存。一段时间后,服务器的内存就被淹没了。 我需要找到一种方法来确保所有流程正确完成。
I have a php script that divides a task into multiple parts and runs each part in a separate child process. The code looks like this:
foreach($users as $k => $arr) {
if(($pid = pcntl_fork()) === -1) continue;
if($pid) {
pcntl_wait($status,WNOHANG);
continue;
}
ob_start();
posix_setsid();
dbConnect();
// do stuff to data
exit();
}
I'm running this script using crontab on a Debian server, but the problem is some processes keep running and reserve memory. After a while the server's memory is flooded.
I need to find a way to make sure all processes finish correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题是在 pcntl_wait 调用中使用 WNOHANG。这意味着 pcntl_wait 函数存在于您想要的子进程之前,以便能够同时分叉其他子进程。但它有一个副作用,即主要父母先于一些孩子完成。此链接 http://www.devshed。 com/c/a/PHP/Managing-Standalone-Scripts-in-PHP/2/ 描述了如何使用 pcntl_wait 和 WNOHANG 进行循环,直到所有子项完成。
I think the issue is the use of WNOHANG in the pcntl_wait call. This means the pcntl_wait function exist before the child process - which you want, in order to be able to fork the other child processes concurrently. But it has the side-effect that the main parent finishes before some of the children. This link http://www.devshed.com/c/a/PHP/Managing-Standalone-Scripts-in-PHP/2/ describes how to loop using pcntl_wait with WNOHANG until all children complete.
您对数据所做的事情需要很长时间或永远。您需要调试您执行的操作。
The stuff you do to the data takes to long or forever. You need to debug the operations you execute.