gpt4 book ai didi

Perl Anyevent ,非阻塞 redis 推送

转载 作者:行者123 更新时间:2023-12-05 00:31:52 25 4
gpt4 key购买 nike

我有下面的代码来做非阻塞 rpush 到 redis 服务器
当我只运行 1 rpush 时,代码工作正常
但是当我在 while 循环中运行它时,脚本在第一次执行后挂起。
为什么 ?

#!/usr/bin/perl                                                                                                                                                          
use AnyEvent;
use AnyEvent::Redis::RipeRedis;
use strict;
#my $cv = AE::cv();

my $redis = AnyEvent::Redis::RipeRedis->new(
host => 'localhost',
port => '6379',
);

my $i=0;

my $cv;
while($i++ < 5) {
$cv = AnyEvent->condvar;
$redis->rpush( 'list', "1","2","3",
{ on_done => sub {
my $data = shift;
print "$data\n";
},
}
);
$cv->recv();
}
$redis->quit(
sub {$cv->send();}
);
$cv->recv();

最佳答案

当您在 while 循环中调用 $cv->recv() 并且脚本正在等待 $cv->send 或 $cv->croak 时,您会阻止脚本执行,但在回调中您不会调用 $cv->send()。

$cv->recv

Wait (blocking if necessary) until the ->send or ->croak methods have been called on $cv, while servicing other watchers normally.


如果您想发送不同的非阻塞请求,请尝试使用 AnyEvents 的开始和结束方法。
#!/usr/bin/perl
use AnyEvent;
use AnyEvent::Redis::RipeRedis;
use strict;

my $redis = AnyEvent::Redis::RipeRedis->new(
host => 'localhost',
port => '6379',
);

my $i=0;

my $cv = AnyEvent->condvar;
while($i++ < 5) {
$cv->begin;
$redis->rpush( 'list', "1","2","3",
{
on_done => sub {
my $data = shift;
print "$data\n";
$cv->end();
},
}
);
}

$cv->recv();

关于Perl Anyevent ,非阻塞 redis 推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37527001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com