在 Perl 中执行多处理任务时出现问题
我正在尝试做一个基本的多处理任务,这就是我所拥有的。首先,我不知道使该程序成为非阻塞进程的正确方法,因为当我等待子进程的响应(使用 waitpid
)时,其他进程也有在队列中等待,但是,如果某些子进程之前死亡(我的意思是,进程无序死亡)会发生什么?所以,我一直在寻找,我发现我可以获取刚刚死亡的进程的PID,为此我使用waitpid(-1, WNOHANG)
。我总是收到警告,指出 WNOHANG 不是数字,但是当我添加 lib sys_wait_h 时,我没有收到该错误,但脚本从不等待 PID,可能是什么错误?
#!/usr/bin/perl #use POSIX ":sys_wait_h"; #if I use this library, I dont get the error, but it wont wait for the return of the child use warnings; main(@ARGV); sub main{ my $num = 3; for(1..$num){ my $pid = fork(); if ($pid) { print "Im going to wait (Im the parent); my child is: $pid\n"; push(@childs, $pid); } elsif ($pid == 0) { my $slp = 5 * $_; print "$_ : Im going to execute my code (Im a child) and Im going to wait like $slp seconds\n"; sleep $slp; print "$_ : I finished my sleep\n"; exit(0); } else { die "couldn’t fork: $!\n"; } } foreach (@childs) { print "Im waiting for: $_\n"; my $ret = waitpid(-1, WNOHANG); #waitpid($_, 0); print "Ive just finish waiting for: $_; the return: $ret \n"; } }
预先感谢,再见!
I'm trying to make a basic multiprocessing task and this is what I have. First of all, I don't know the right way to make this program as a non-blocking process, because when I am waiting for the response of a child (with waitpid
) the other processes also have to wait in the queue, but, what will happen if some child processes die before (I mean, the processes die in disorder)? So, I've been searching and I foud that I can get the PID of the process that just die, for that I use waitpid(-1, WNOHANG)
. I always get a warning that WNOHANG
is not a number, but when I added the lib sys_wait_h, I didn't get that error but the script never waits for PID, what may be the error?
#!/usr/bin/perl #use POSIX ":sys_wait_h"; #if I use this library, I dont get the error, but it wont wait for the return of the child use warnings; main(@ARGV); sub main{ my $num = 3; for(1..$num){ my $pid = fork(); if ($pid) { print "Im going to wait (Im the parent); my child is: $pid\n"; push(@childs, $pid); } elsif ($pid == 0) { my $slp = 5 * $_; print "$_ : Im going to execute my code (Im a child) and Im going to wait like $slp seconds\n"; sleep $slp; print "$_ : I finished my sleep\n"; exit(0); } else { die "couldn’t fork: $!\n"; } } foreach (@childs) { print "Im waiting for: $_\n"; my $ret = waitpid(-1, WNOHANG); #waitpid($_, 0); print "Ive just finish waiting for: $_; the return: $ret \n"; } }
Thanks in advance, bye!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 WNOHANG,如果没有子进程终止,该进程将不会阻塞。这就是WNOHANG的意义所在;它确保 waitpid() 快速返回。就您而言,您似乎只想使用 wait() 而不是 waitpid()。
If you use WNOHANG, the process will not block if no children have terminated. That's the point of WNOHANG; it ensures that waitpid() will return quickly. In your case, it looks like you want to just use wait() instead of waitpid().
我发现 POE 很好地为我处理了所有这些事情。它是对各种事物(包括外部进程)的异步(非阻塞)控制。您不必处理所有低级事务,因为 POE 会为您完成。
I find that POE handles all of this stuff for me quite nicely. It's asynchronous (non-blocking) control of all sorts of things, including external processes. You don't have to deal with all the low level stuff because POE does it for you.