Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I haven't had need for Unix IPC in a while, so I think there's some easy way to what I need that I'm overlooking. So anything that works is great, but especially clever solutions as always, get bonus points.

I have a load-testing script that makes a specific web request repeatedly until a stop condition is satisfied (the server is supposed to eventually get fed up and stop sending the normal response when too many requests come in from the same IP... and yes, I know that's a terrible way to fend off a DoS). I'd like to be able to run many copies of the script in parallel without actually opening ten xterms and running the script in each. This is easy with fork as long as the children and parent don't need to communicate, but I'd like to have each child report back to the parent periodically saying how many requests it's made so far. The parent would then sum up the requests made from the children and report to stdout on how many requests had been made in total. Execution order of the children isn't relevant.

Looking at perlipc and answers to older questions, I'm thinking about something in the neighborhood of this:

# ... setup code my $NUM_CHILDREN = 10; my $total_count; my @pids; for (my $i = 0; $i < $NUM_CHILDREN; $i++) { $pids[$i] = open(CHILD, "-|"); if ($pids[$i]) { next; } elsif (defined $pid) { # child handler, $pid=0 my $count = 0; my $done = 0; while (!$done) { # make web request; set $done somewhere $count++; } } else { die "Fork failed at number $i: $!\n"; } }
As written, the parent process spawns all the children and exits, which 1) doesn't print hit counts like I want, and 2) doesn't allow me to cancel all the children at once, which I'd like to do. I'm unsure of where and how to send $count up to the parent from the children, and how the parent would make use of that. I'm also not quite sure how to have the parent kill the children when it dies abnormally. Any clues?

In reply to Methods for Asynchronous IPC by athomason

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 romping around the Monastery: (2)
As of 2024-04-24 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found