Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Perhaps something as simple as forking is all you need. Here's an example that kind of simulates what I think you're trying to do. Essentially, we create an API object with a couple of methods. Instead of calling them directly, we pass the object and the sub we want to call on it to a forking routine from within a local sub (save(), users()), and then let it work in the background while the main script carries on.

At the end of the script, we wait until all forked processes have completed before we exit.

use warnings; use strict; package Thing; { sub new { return bless {}, shift; } sub users { print "in ". __PACKAGE__ . " execing users()\n"; sleep 2; # simulate long API call print "done API users()\n"; } sub save { print "in ". __PACKAGE__ . " execing save()\n"; sleep 2; # simulate long API call print "done API save()\n"; } } package main; use Parallel::ForkManager; my $max_forks = 10; my $fork = new Parallel::ForkManager($max_forks); my $api = Thing->new; print "doing stuff in main...\n"; print "calling main::save()...\n"; save($api); print "continuing in main, save() API call isn't blocking\n"; print "calling main::users()...\n"; users($api); print "finished in main, waiting for forks to finish\n"; $fork->wait_all_children; sub save { my $api = shift; fork_api_call($api, 'save'); print "forked API save...\n"; } sub users { my $api = shift; fork_api_call($api, 'users'); print "forked API users...\n"; } sub fork_api_call { my ($api, $sub) = @_; for (0..$max_forks){ $fork->start and last; $api->$sub(); $fork->finish; } }


doing stuff in main... calling main::save()... forked API save... continuing in main, save() API call isn't blocking calling main::users()... in Thing execing save() forked API users... finished in main, waiting for forks to finish in Thing execing users() done API save() done API users()

In reply to Re^3: Mojo: non-blocking API calls by stevieb
in thread Mojo: non-blocking API calls by Beatnik

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (2)
    As of 2021-05-09 06:36 GMT
    Find Nodes?
      Voting Booth?
      Perl 7 will be out ...

      Results (100 votes). Check out past polls.