Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fork a Perl CGI program to hive off long-running tasks?

Tags:

fork

cgi

perl

I am writing a Bulk Mail scheduler controlled from a Perl/CGI Application and would like to learn abut "good" ways to fork a CGI program to run a separate task? Should one do it at all? Or is it better to suffer the overhead of running a separate job-queue engine like Gearman or TheSchwartz as has been suggested recently. Does the answer/perspective change when using an near-MVC framework like CGI::Application over vanilla CGI.pm? The last comes from a possible project that I have in mind for a CGI::Application Plugin - that would make "forking" a process relatively simple to call.

like image 736
Ya. Perelman Avatar asked Jan 24 '23 13:01

Ya. Perelman


1 Answers

Look at Proc::Daemon - it's the simplest thing that works. From your CGI script, do the CGI business (getting input, returning a response to the browser), then call Proc::Daemon::init() which does the fork, daemonizes your process and makes the parent exit. Then your script (now a daemon) does its long-running tasks and exits when they're done. You'll want to update something (file, database record) while running as a daemon, so subsequent CGI invocations can check what it did (or how it's progressing).

like image 105
Dotan Dimet Avatar answered Jan 30 '23 01:01

Dotan Dimet