How can I fork a background processes from a Perl CGI script on Windows?

前端 未结 5 817
自闭症患者
自闭症患者 2020-12-03 19:29

I\'ve had some trouble forking of processes from a Perl CGI script when running on Windows. The main issue seems to be that \'fork\' is emulated when running on windows, and

相关标签:
5条回答
  • 2020-12-03 19:52

    Use Win32::Process->Create with DETACHED_PROCESS parameter

    0 讨论(0)
  • 2020-12-03 19:53

    If you want to do this in a platform independent way, Proc::Background is probably the best way.

    0 讨论(0)
  • 2020-12-03 19:56

    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.

    0 讨论(0)
  • 2020-12-03 19:57

    I've found real problems with fork() on Windows, especially when dealing with Win32 Objects in Perl. Thus, if it's going to be Windows specific, I'd really recommend you look at the Thread library within Perl.

    I use this to good effect accepting more than one connection at a time on websites using IIS, and then using even more threads to execute different scripts all at once.

    0 讨论(0)
  • 2020-12-03 20:00

    This question is very old, and the accepted answer is correct. However, I just got this to work, and figured I'd add some more detail about how to accomplish it for anyone who needs it.

    The following code exists in a very large perl CGI script. This particular sub routine creates tickets in multiple ticketing systems, then uses the returned ticket numbers to make an automated call via Twilio services. The call takes awhile, and I didn't want the CGI users to have to wait until the call ended to see the output from their request. To that end, I did the following:

      (All the CGI code that is standard stuff.  Calls the subroutine needed, and then)
    
      my $randnum = int(rand(100000));
      my $callcmd = $progdir_path . "/aoff-caller.pl --uniqueid $uuid --region $region --ticketid $ticketid";
      my $daemon = Proc::Daemon->new(
        work_dir     => $progdir_path,
        child_STDOUT => $tmpdir_path . '/stdout.txt',
        child_STDERR => $tmpdir_path . '/stderr.txt',
        pid_file     => $tmpdir_path . '/' . $randnum . '-pid.txt',
        exec_command => $callcmd,
      );
      my $pid = $daemon->Init();
    
      exit 0;
    
      (kill CGI at the appropriate place)
    

    I am sure that the random number generated and attached to the pid is overkill, but I have no interest in creating issues that are extremely easily avoided. Hopefully this helps someone looking to do the same sort of thing. Remember to add use Proc::Daemon at the top of your script, mirror the code and alter to the paths and names of your program, and you should be good to go.

    0 讨论(0)
提交回复
热议问题