首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AnyEvent->定时器不能与AnyEvent::Handle一起工作?

AnyEvent->定时器不能与AnyEvent::Handle一起工作?
EN

Stack Overflow用户
提问于 2014-11-03 20:24:32
回答 1查看 467关注 0票数 4

我正在尝试在我的Catalyst,AnyEvent,Websocket应用程序中构建一个超时场景。为此,我使用了

代码语言:javascript
复制
AnyEvent->timer

它应该在几秒钟不活动之后调用(不再有WS帧进入)。

问题是,我的计时器永远不会执行:

代码语言:javascript
复制
my $w = AnyEvent->timer (after => 3,
                         cb    => sub {
    warn "TIMEOUT!";
});

$self->{server} = Protocol::WebSocket::Handshake::Server->new_from_psgi(
                            $c->req->env) or die $c->log->fatal($!);

$self->{handle} = AnyEvent::Handle->new(
    fh => $c->req->io_fh,
    on_error => sub {
        my ($hd, $fatal, $msg) = @_;
        $clean_up->();
    }
);

die $c->log->fatal("WS Server error: '$_'")
        if $self->{server}->error;

$self->{server}->parse($self->{handle}->fh);
$self->{handle}->push_write($self->{server}->to_string);

$self->{handle}->on_read(sub {
    (my $frame = $self->{server}->build_frame)->append($_[0]->rbuf);

    while (my $frame_msg = $frame->next) {
        ...
    }

计时器回调永远不会执行。我的猜测是,计时器不能在另一个事件循环(AnyEvent::Handle)中工作?

EN

回答 1

Stack Overflow用户

发布于 2014-12-12 15:21:02

您是否真的进入了要处理的计时器的事件循环中?您的代码片段没有指明这一点。

此外,AnyEvent::Handle还内置了不活动超时:

timeout => $fractional\_seconds If non-zero, then this enables an "inactivity" timeout: whenever this many seconds pass without a successful read or write on the underlying file handle, the "on\_timeout" callback will be invoked (and if that one is missing, a non-fatal "ETIMEDOUT" error will be raised). Note that timeout processing is also active when you currently do not have any outstanding read or write requests: If you plan to keep the connection idle then you should disable the timout temporarily or ignore the timeout in the "on\_timeout" callback, in which case AnyEvent::Handle will simply restart the timeout. Zero (the default) disables this timeout. on\_timeout => $cb->($handle) Called whenever the inactivity timeout passes. If you return from this callback, then the timeout will be reset as if some activity had happened, so this condition is not fatal in any way.

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26714292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档