Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Your fork_bot doesn't return until it has slept.

You need to fork both off, and manage the sleep events in the parent. And the parent has to wait around for the children to finish, else the parent exiting may also terminate its children.

Personally, I've found that using an event manager simplifies this greatly. Completely untested code:

use strict; use warnings; use AnyEvent; use AnyEvent::Util; sub fork_bot { my $arg = shift; my $pid; my $cv = AnyEvent::Util::run_cmd( $arg->{bot}, '$$' => \$pid, ); my $w; $w = AE::timer( $arg->{runtime}, 0, sub { print "Killing $arg->{runtime} - timeout +\n"; kill INT, $pid; $w = undef; } ); $cv } my $bot1 = fork_bot({ bot => ['./cfbot.pm', '--debug'], runtime => 60 +}); my $bot2 = fork_bot({ bot => ['./cfbot_tester.pm'], runtime => 20 +}); $bot1->recv; $bot2->recv; # continue with program
Now, I'm sure you can do this equally as well with other event handlers, or with threads. This is just what I'm used to.

Coro can simplify this a bit further, but at this point it's not enough to warrant, IMO. (It would invert the timeout into a Coro-version of sleep, but I don't think that's a huge benefit here.)

Also, we can use this to also clean up the $w watcher, to eliminate that timer if the child exits before the timer runs out.

Hope that helps.


In reply to Re: Forking two processes in parallel by Tanktalus
in thread Forking two processes in parallel by neilwatson

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-19 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found