使用AnyEvent创建单个线程服务器(Perl)

时间:2012-11-06 22:59:06

标签: perl webserver anyevent

我正在创建一个本地服务来侦听localhost并提供一个基本的调用和响应类型接口。我想要开始的是一个婴儿服务器,您可以通过telnet连接并回应它收到的内容。

我听说过AnyEvent很棒,但AnyEvent :: Socket的文档并没有给出一个很好的例子。我想用AnyEvent,AnyEvent :: Socket和AnyEvent :: Handle构建它。

现在小服务器代码如下所示:

#!/usr/bin/env perl

use AnyEvent;
use AnyEvent::Handle;
use AnyEvent::Socket;

my $cv = AnyEvent->condvar;

my $host = '127.0.0.1';
my $port = 44244;

tcp_server($host, $port, sub {
    my($fh) = @_;

    my $cv = AnyEvent->condvar;

    my $handle;
    $handle = AnyEvent::Handle->new(
        fh => $fh,
        poll => "r",
        on_read => sub {
             my($self) = @_;
             print "Received: " . $self->rbuf . "\n";
             $cv->send;
        }
    );

    $cv->recv;
});

print "Listening on $host\n";

$cv->wait;

这不起作用,如果我telnet到localhost:44244我得到了这个:

EV: error in callback (ignoring): AnyEvent::CondVar: 
recursive blocking wait attempted at server.pl line 29.

我想如果我理解如何创建一个小型单线程服务器,我可以通过telnet连接并打印出它给出的任何内容,然后等待更多输入,我可以从那里进一步了解它。有什么想法吗?

3 个答案:

答案 0 :(得分:6)

你在回调中阻止了。这是不允许的。有几种方法可以解决这个问题。我的偏好是从tcp_server回调中启动Coro线程。但是如果没有Coro,这样的事情可能就是你想要的:

#!/usr/bin/env perl5.16.2

use AnyEvent;
use AnyEvent::Handle;
use AnyEvent::Socket;

my $cv = AE::cv;

my $host = '127.0.0.1';
my $port = 44244;

my %connections;
tcp_server(
           $host, $port, sub {
               my ($fh) = @_;

               print "Connected...\n";

               my $handle;
               $handle = AnyEvent::Handle->new(
                                               fh => $fh,
                                               poll => 'r',
                                               on_read => sub {
                                                   my ($self) = @_;
                                                   print "Received: " . $self->rbuf . "\n";
                                               },
                                               on_eof => sub {
                                                   my ($hdl) = @_;
                                                   $hdl->destroy();
                                               },
                                              );
               $connections{$handle} = $handle; # keep it alive.

               return;
           });

print "Listening on $host\n";

$cv->recv;

请注意,我只等待一个condvar。我正在存储句柄以使AnyEvent :: Handle对象保持更长时间。清理$ self-> rbuf的工作留给了读者: - )

问题交叉发布,answer, too: - )

答案 1 :(得分:1)

我也听说过关于AnyEvent的好东西,但还没有用过它。我以前使用IO :: Select编写了一个小型非阻塞服务器。 documentation for that module中有一个例子(我添加了几行):

use IO::Select;
use IO::Socket;

$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
$sel = new IO::Select( $lsn );

while(@ready = $sel->can_read) {
    foreach $fh (@ready) {
        if($fh == $lsn) {
            # Create a new socket
            $new = $lsn->accept;
            $sel->add($new);
        }
        else {
            # Process socket
            my $input = <$fh>;
            print $fh "Hello there. You said: $input\n";

            # Maybe we have finished with the socket
            $sel->remove($fh);
            $fh->close;
        }
    }
}

答案 2 :(得分:0)

我不确定你的condvar试图在那里触发什么。用它来发送状态,如:

#!/usr/bin/env perl

use AnyEvent;
use AnyEvent::Handle;
use AnyEvent::Socket;

my $host = '127.0.0.1';
my $port = 44244;

my $exit = AnyEvent->condvar;

tcp_server($host, $port, sub {
    my($fh) = @_;

    my $handle; $handle = AnyEvent::Handle->new(
        fh => $fh,
        poll => "r",
        on_read => sub {
             my($self) = @_;
             print "Received: " . $self->rbuf . "\n";
             if ($self->rbuf eq 'exit') {
               $exit->send;
             }
        }
    );

});

print "Listening on $host\n";

$exit->recv;