如何在Perl中解雇一个进程? [英] How can I fire and forget a process in Perl?

查看:86
本文介绍了如何在Perl中解雇一个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何在Perl中解雇一个进程吗?我已经看过 ruby​​:如何触发并忘记子流程? 在Ruby中执行相同的操作.

Can somebody please tell me how to fire-and-forget a process in Perl? I've already looked at ruby: how to fire and forget a subprocess? for doing the same in Ruby.

推荐答案

来自 perlfaq8 的答案如何启动后台处理?

几个模块可以启动其他不会阻塞您的Perl的进程 程序.您可以使用IPC :: Open3,Parallel :: Jobs,IPC :: Run和其他一些功能 POE模块.有关更多详细信息,请参见CPAN.

Several modules can start other processes that do not block your Perl program. You can use IPC::Open3, Parallel::Jobs, IPC::Run, and some of the POE modules. See CPAN for more details.

您也可以使用

system("cmd &")

或者您可以使用perlfunc的"fork"中所述的fork,进一步 perlipc中的示例.如果您使用的是Unix,则需要注意一些事项- 像系统一样:

or you could use fork as documented in "fork" in perlfunc, with further examples in perlipc. Some things to be aware of, if you're on a Unix- like system:

STDIN,STDOUT和STDERR已共享

主要流程和后台流程(子级" 进程)共享相同的STDIN,STDOUT和STDERR文件句柄.如果 两者都尝试一次访问它们,否则可能会发生奇怪的事情.你 可能要为孩子关闭或重新打开这些门.你可以得到 通过打开"管道解决此问题(请参阅perlfunc中的打开"),但是 在某些系统中,这意味着子进程不能超过 父母.

Both the main process and the backgrounded one (the "child" process) share the same STDIN, STDOUT and STDERR filehandles. If both try to access them at once, strange things can happen. You may want to close or reopen these for the child. You can get around this with "open"ing a pipe (see "open" in perlfunc) but on some systems this means that the child process cannot outlive the parent.

信号

您将必须捕获SIGCHLD信号,并且可能还捕获SIGPIPE. 后台进程完成后,将发送SIGCHLD. SIGPIPE是 当您写入其子进程已关闭的文件句柄时发送 (未捕获的SIGPIPE可能会导致您的程序无声地死掉). 这不是"system("cmd&"))"的问题.

You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too. SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is sent when you write to a filehandle whose child process has closed (an untrapped SIGPIPE can cause your program to silently die). This is not an issue with "system("cmd&")".

僵尸

您必须准备在子进程收获"时准备 完成.

You have to be prepared to "reap" the child process when it finishes.

   $SIG{CHLD} = sub { wait };

   $SIG{CHLD} = 'IGNORE';

您也可以使用双叉.您立即等待() 第一个孩子,init守护程序将为您的孙子等待() 一旦退出.

You can also use a double fork. You immediately wait() for your first child, and the init daemon will wait() for your grandchild once it exits.

   unless ($pid = fork) {
       unless (fork) {
           exec "what you really wanna do";
           die "exec failed!";
       }
       exit 0;
   }
   waitpid($pid, 0);

有关执行此操作的其他代码示例,请参见perlipc中的信号". 僵尸不是"system(" prog&)"的问题.

See "Signals" in perlipc for other examples of code to do this. Zombies are not an issue with "system("prog &")".

这篇关于如何在Perl中解雇一个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆