Domanda

So I've been having a difficult time with my foray into event driven programming. Most of it due to still thinking sequentially but I'm having a hard time understanding how anyone synchronizes their code when using LWP::Protocol::AnyEvent::http and am looking for some help in understanding. Here is the smallest program I can create that demonstrates my basic lack of understanding:

use strict;
use warnings;

use 5.10.0;

use LWP::Protocol::AnyEvent::http;
use WWW::Mechanize;
use Coro qw(async);

my $url = "http://feedproxy.google.com/~r/PerlNews/~3/kqUb_rpU5dE/";

my $mech = WWW::Mechanize->new;
$mech->get($url);

my @cs;
foreach my $link ($mech->links) {
  my $c = async {
    say "Getting " . $link->url;
    my $ua = WWW::Mechanize->new;
    $ua->get($link->url);
  };
  push(@cs, $c);
} 

$_->join for (@cs);

How do I make sure the ->get has succeeded before going into the foreach loop? the ->get will return immediately since it doesn't block when using the LWP::Protocol::AnyEvent::http module. So there are no ->links and the program just exits. Removing LWP::Protocol::AnyEvent::http obviously makes the program return links, like a regular sequential program, and slow like one too.

Thanks for any insight.

È stato utile?

Soluzione

the ->get will return immediately since it doesn't block when using the LWP::Protocol::AnyEvent::http module.

That's not true. It blocks as normal. LWP::Protocol::AnyEvent::http should not affect how WWW::Mechanize works at all. It merely allows other Coro threads and AnyEvent callbacks to execute while WWW::Mechanize is blocked.

How do I make sure the ->get has succeeded before going into the foreach loop?

Your code already waits for it to complete. (In fact, I'm tempted to add your code to the documentation!)

If you want to check if it succeeded, you could use

die "Error" if !$mech->success;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top