【发布时间】:2018-09-09 22:45:22
【问题描述】:
我有以下代码:
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 秒?
为什么阻塞代码比非阻塞代码快?
【问题讨论】:
-
我重温了这个问题
标签: perl mojolicious mojo-useragent