如何在Windows上从Perl CGI脚本派生后台进程?

时间:2020-03-05 18:48:13  来源:igfitidea点击:

在Windows上运行时,从Perl CGI脚本分叉进程时遇到了一些麻烦。主要问题似乎是在Windows上运行时模拟了" fork",实际上似乎并没有创建一个新进程(只是当前进程中的另一个线程)。这意味着正在等待进程完成的Web服务器(如IIS)将继续等待,直到"后台"进程完成。

有没有办法在Windows下从CGI脚本派生后台进程?更好的是,有没有一个我可以调用的函数可以跨平台实现?

(而且,为了使生活变得更加困难,我真的希望有一个很好的方法可以将分叉的进程同时重定向到文件中)。

解决方案

回答

perlfork:

Perl provides a fork() keyword that
  corresponds to the Unix system call of
  the same name. On most Unix-like
  platforms where the fork() system call
  is available, Perl's fork() simply
  calls it.
  
  On some platforms such as Windows
  where the fork() system call is not
  available, Perl can be built to
  emulate fork() at the interpreter
  level. While the emulation is designed
  to be as compatible as possible with
  the real fork() at the the level of
  the Perl program, there are certain
  important differences that stem from
  the fact that all the pseudo child
  ``processes'' created this way live in
  the same real process as far as the
  operating system is concerned.

回答

使用Win32 :: Process-> Create with DETACHED_PROCESS参数

回答

如果要以独立于平台的方式执行此操作,则Proc :: Background可能是最好的方式。

回答

我在Windows上发现了fork()的真正问题,尤其是在Perl中处理Win32对象时。因此,如果它将特定于Windows,我真的建议我们查看Perl中的Thread库。

我使用此方法效果很好,一次可以在使用IIS的网站上接受多个连接,然后使用更多的线程一次执行所有不同的脚本。