Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Can I have a Perl script, initiated from a browser, fork itself, and not wait for the child to end?

by dave_the_m (Monsignor)
on Apr 07, 2022 at 08:24 UTC ( [id://11142787] : note . print w/replies, xml ) Need Help??


in reply to Can I have a Perl script, initiated from a browser, fork itself, and not wait for the child to end?

The direct answer to your question is that the filehandles which form the connection to the web client get duplicated in the child, so the connection doesn't get closed until both the parent and child have closed the filehandles. So normally the parent would fork, the child would immediately close any filehandles it shares with the parent, then go off and do its thing. In the meantime the parent handles doing any HTML output then quits.

Note that you show both the parent and child outputting the headers, which is wrong.

But more generally this arrangement is likely to be a bad idea. It's very easy for someone to maliciously or inadvertently send many short requests to the web server, which results in thousands of long-running forked processes vying for memory and CPU.

Dave.