AnyEvent with Coro, AKA async TCP daemon.

Andrew Whatson whatson at gmail.com
Fri Aug 2 03:59:30 CEST 2013


Hi Antti,

You are using a blocking sleep() in your threads, so no work can happen
until the sleep completes.  You should use Coro::AnyEvent::sleep()
instead, which will allow other threads to run in the interim.

 https://metacpan.org/module/Coro::AnyEvent

Cheers,
Andrew

On Thu, Aug 1, 2013 at 11:57 PM, Antti Linno <sihker at hot.ee> wrote:
> Good day.
>
> I'm trying to build a simple non-blocking TCP daemon. If I need HTTP daemon,
> should I use Twiggy?
>
> This is what I have so far:
>
> use Modern::Perl '2012';
> use EV;
> use AnyEvent::Socket;
> use Coro;
>
> sub start_thread($$$$) {
>     my ( $time, $fh, $host, $port ) = @_;
>     return async {
>         say "Starting sleep of $time, $host, $port";
>         sleep $time;
>         say "Done sleeping!";
>     };
> }
>
> AnyEvent::Socket::tcp_server undef, 11211, sub {
>   my ( $fh, $host, $port ) = @_;
>
>   start_thread 20, $fh, $host, $port;
>   }, sub {
>   my ( $fh, $thishost, $thisport ) = @_;
>   AE::log info => "Bound to $thishost, port $thisport.";
>   };
>
> EV::loop;
>
> The problem is, this solution is blocking. If I make two requests, then the
> second one is pending, while the first one sleeps.
>
> I could fork
>
> sub start_thread($$$$) {
>     my ( $time, $fh, $host, $port ) = @_;
>     if ( my $pid = fork ) {
>         say "Starting sleep of $time, $host, $port";
>         sleep 20;
>         say "Done sleeping!";
>     };
> }
>
> This is not blocking, but is this the optimal way? How to do it with Coro?
>
> Greetings,
> Antti
>
> _______________________________________________
> anyevent mailing list
> anyevent at lists.schmorp.de
> http://lists.schmorp.de/mailman/listinfo/anyevent



More information about the anyevent mailing list