[cgiapp] Tasks after fork()'ing a CGI::Application
Michael Peters
mpeters at plusthree.com
Mon Aug 10 11:18:31 EDT 2009
Gurunandan R. Bhat wrote:
> I would like this to be as robust as possible. Currently, all I do in
> the child before launching the long-running process is close STDOUT. Is
> there any other tasks I must perform?
Make sure you don't have have any other file handles in use. This includes
sockets and database handles.
> Is there a CPAN module that might
> help clean up things in the child better than this? I tried Proc::Daemon
> but that closes all file descriptors inherited from the parent(including
> a few datafile descriptors that I need access to). I can of course
> re-open them from well know locations.
You really should re-open them. Strange things can happen when your system gets
under a decent load that aren't apparent when testing or under a light load when
you share file handles/sockets across processes like that.
> Any advice on making this method more robust would be hugely
> appreciated.
I would actually suggest that you don't fork your web process to do background
tasks. The problem is that your web processes are generally much heavier than
you need which means wasted resources. It also can cause scalability concerns if
you suddenly have lots of background tasks to perform. I'd suggest you look into
having a dedicated job queue. You could use one of the existing frameworks like
Gearman or TheSchwartz, or you could write you own using something like POE or
Proc::Daemon+Parallel::ForkManager.
--
Michael Peters
Plus Three, LP
More information about the cgiapp
mailing list