For Event, the child will inherit the timers. This is not a problem though since you do not want the child to return control back to Event.

So at the end of the work done in the child, you exit. You can unloop before or after the child does its work too ... though I agree it does not seem clear from the Event docs what this buys you or whether it is preferred.

If you 'return' at the end of the child, with no unlooping, you end up with 2 event loops running and the same configured watchers. If you unloop, you continue from after where you called Event::loop.

Some code to illustrate the idea:

1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 use Event; 7 use POSIX ":sys_wait_h"; 8 9 Event->signal(signal=>'CHLD', cb=>sub{ 10 my $kid; 11 do { 12 $kid = waitpid(-1, WNOHANG); 13 } while ($kid > 0); 14 }); 15 16 Event->timer(after=>1, interval=>2, cb=>sub { 17 print "$$: timer\n"; 18 my $pid = fork(); 19 return unless (defined($pid)); 20 return if ($pid); 21 22 print "child: $$\n"; 23 `sleep 5`; 24 exit(); # you can also call Event::unloop here. 25 26 }); 27 28 Event::loop(); 29 print "Unloop resumes code here\n";

In reply to Re: Event-based app and fork() by reenen
in thread Event-based app and fork() by powerman

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.