Are forks needed for this, or could you be making your API requests using a non-blocking user agent?

Forks have their own overhead, and when you make an HTTP request against an API, most of your time is spent just sitting around waiting for the response -- you're not CPU bound. If this describes your situation, have a look at Mojo::UserAgent, Mojo::IOLoop, and Mojo::Promise. There are other options: AnyEvent::UserAgent, for example, but Mojo::UserAgent makes such tidy work of the problem, it's a really nice option.

Of course I don't know what type of API you're hitting. Hopefully this suggestion is useful in your case. Example:

my @promises = map {$ua->get_p($_)} @api_points; Mojo::Promise->all(@promises)->then(sub { my (@results) = @_; print Dumper $_->[0]->{'result'}->json foreach @results; });

If you have six @api_points this will make six GET requests in rapid fire and then process the results when they all return. There's a little code that I didn't show that surrounds this; using Mojo::UserAgent, getting a $ua object, kicking off the event loop if it hasn't started, that sort of thing. But it's all documented in the links above.


Dave


In reply to Re: Parallel::ForkManager right approach by davido
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.