Open file handles, open database transactions and (other) open network connections. For forking to be clean without side effects, i would need to close all those, fork and then reopen them in the parent process.

My worker processes are cyclic executives that run many modules, each doing it's own thing. The main loop is basically something like this:

... ##### MAIN TIMING LOOP ##### sub run($self) { my $runok = 0; eval { # Let STDOUT/STDERR settle down first sleep(0.1); my $nextCycleTime = $self->{config}->{mincycletime} + time; while(1) { my $workCount = $self->{worker}->run(); my $now = time; if($now < $nextCycleTime) { my $sleeptime = $nextCycleTime - $now; #print "** Fast cycle ($sleeptime sec to spare), sleep +ing **\n"; sleep($sleeptime); $nextCycleTime += $self->{config}->{mincycletime}; #print "** Wake-up call **\n"; } else { #print "** Slow cycle **\n"; $nextCycleTime = $self->{config}->{mincycletime} + $no +w; } } $runok = 1; }; if(!$runok) { suicide('RUN FAILED', $EVAL_ERROR); } return; } ... #### WORKER CYCLE #### sub run($self) { my $workCount = 0; # Run cleanup functions in case the last cycle bailed out with cro +ak foreach my $worker (@{$self->{cleanup}}) { my $module = $worker->{Module}; my $funcname = $worker->{Function} ; #$workCount += $module->$funcname(); $module->$funcname(); } # Notify all registered workers about dead children while((my $child = shift @deadchildren)) { foreach my $worker (@{$self->{sigchld}}) { my $module = $worker->{Module}; my $funcname = $worker->{Function} ; $workCount++; $module->$funcname($child); } } # Run all worker functions foreach my $worker (@{$self->{workers}}) { my $module = $worker->{Module}; my $funcname = $worker->{Function} ; $workCount += $module->$funcname(); } # Run cleanup functions foreach my $worker (@{$self->{cleanup}}) { my $module = $worker->{Module}; my $funcname = $worker->{Function} ; #$workCount += $module->$funcname(); $module->$funcname(); } return $workCount; } ...

It may not be very elegant, but it's simple, easy to debug and easy to balance worker loads by moving module configurations between different XML config files. Yeah, the stuff dynamically configures itself on startup.

Yes, depending on strict limits to the configuration of a specific worker, i can support forking. But this makes it a pain to work with, which is why i have been going to full "use non-blocking stuff" route for the last decade in my workers. Most of the stuff is low level protocols or web APIs that are already fire-and-check-back-later. I haven't encountered a web API that blocks for up to a minute in like 15 years, so i was unaware which modules were available to solve/circumvent that specific problem.

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Also check out my sisters artwork and my weekly webcomics

In reply to Re^2: LWP::UserAgent non-blocking calls by cavac
in thread LWP::UserAgent non-blocking calls by cavac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.