我有以下代码:
my $ua = Mojo::UserAgent->new ();
my @ids = qw(id1 id2 id3);
foreach (@ids) {
my $input = $_;
my $res = $ua->get('http://my_site/rest/id/'.$input.'.json' => sub {
my ($ua, $res) = @_;
print "$input =>" . $res->result->json('/net/id/desc'), "\n";
});
}
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
为什么当我运行上面的代码(非阻塞)时,在运行代码作为阻塞时需要大约 6 秒,即在循环内部类似于:
my $res = $ua->get('http://my_site/rest/id/'.$input.'.json');
print "$input =>" . $res->result->json('/net/id/desc'), "\n";
没有最新的线路大约需要 1 秒?
为什么阻塞代码比非阻塞代码快?