I can find lots of examples on how to set up child process spawning/forking for a client/server model, but I need to set up child process spawning within a script that's run only once.
The basic process is "for each item in a list, script calls a subroutine with that item as input, and returns the corresponding output". I need to simultaneously execute the subroutine for each item in the list.
Using the "spawn" subroutine on page 351 of Programming Perl, I tried this:
my $output;
foreach my $item (@items) {
&spawn(sub { $output .= &search($item); });
}
This produces, at best, a looping that I couldn't begin to debug.
I know this has been done before - multi-threaded searches, etc. seem commonplace. Anyone have any code snippets?
In reply to Fork/Spawn
by Silas
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.