There is/was Coro::LWP, which integrated LWP with Coro, but so far I have not found a good way to make LWP::UserAgent-based scripts parallel without rewriting them.

I am fond of using Future, but also had good success using Mojo::UserAgent. Both have APIs somewhat different from LWP::UserAgent though.

With both systems, the logic would very much like the approach you outlined above:

# Using (my own) Future::HTTP, which provides an AnyEvent::HTTP-like A +PI my $ua = Future::HTTP->new(); # Fire off all requests at once, rate limiting etc. is left as an exer +cise my @outstanding; for my $url (@requests) { my $res = $ua->http_get($url)->then(sub { my( $body, $data ) = @_; # ... handle the response return $body }); push @outstanding, $res; }; while( @outstanding ) { my $body = (shift @outstanding)->get; ... };

With Mojolicious you can look at COWS::Crawler, which also implements something like the following API:

my $crawler = COWS::Crawler->new(); $crawler->submit_request({ method => 'GET', url => $url, info => { u +rl => $url }} ); while( my ($page) = $crawler->next_page ) { my $body = $page->{res}->body; my $url = $page->{req}->req->url; ... };

But all of these approaches need a real rewrite of the formerly sequential program logic into asynchronous execution.


In reply to Re: LWP::UserAgent non-blocking calls by Corion
in thread LWP::UserAgent non-blocking calls by cavac

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.