Takamoto:

To test your code, I added this to the end:

sub tprint { my $t = time; print "$t: ", shift, "\n"; } sub getResultsAPI { my ($name, $doze_time) = @_; tprint "API_$name: dozing for ${doze_time}s"; sleep $doze_time; tprint "API_$name: done"; } sub getResultsAPI_1 { getResultsAPI(1, 12) } sub getResultsAPI_2 { getResultsAPI(2, 22) } sub getResultsAPI_3 { getResultsAPI(3, 16) } sub getResultsAPI_4 { getResultsAPI(4, 5) } sub getResultsAPI_5 { getResultsAPI(5, 11) } sub getResultsAPI_6 { getResultsAPI(6, 7) }

And it seemed to do what you'd expect:

$ perl pm_11107400.pl 1570989189: API_1: dozing for 12s 1570989189: API_2: dozing for 22s 1570989189: API_3: dozing for 16s 1570989189: API_4: dozing for 5s 1570989189: API_5: dozing for 11s 1570989194: API_4: done 1570989200: API_5: done 1570989201: API_1: done 1570989205: API_3: done 1570989211: API_2: done

Of course, if your APIs contend with each other for resources (such as hitting the hard drive a lot), you may not save much time. If they run well in parallel as the simple sleep timers do in the case I tried, you can save much more. Make sure you look at what your different APIs do to see if they conflict with each other. Bundling IO-heavy operations with CPU-heavy operations tends to work well to save you time. Similarly, you can *lose* time if you have processes always contending for the same resources.

Note that you have a minor bug, though, in that you never run API_6. I'm assuming the bug was introduced by your simplification of the test code.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Parallel::ForkManager right approach by roboticus
in thread Parallel::ForkManager right approach by Takamoto

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.