AnyEvent with Coro, AKA async TCP daemon.

Marc Lehmann schmorp at
Fri Aug 2 05:27:27 CEST 2013

On Thu, Aug 01, 2013 at 04:57:59PM +0300, Antti Linno <sihker at> wrote:
> Good day.
> If I need HTTP daemon, should I use Twiggy?

(no idea)

> This is what I have so far:

not bad...

>         sleep $time;

This blocks.

A simple workaround is to use Coro::AnyEvent::sleep (or generally the
functions from Coro::AnyEvent, or, if you are sure you will always use EV,
you can use Coro::EV).

> This is not blocking, but is this the optimal way? How to do it with Coro?

Forking can be useful if you hit something that is hard to "make not block".
In that case, AnyEvent::Fork or it's submodules might help.

For AnyEvent and Coro, you need to write your program event based. Coro
allows you to invert the normal structure of an event-based program ("call me
on event x") into a more process-like flow ("wait here for event x"), but
that's about it.

For sleep, see above. For dpoing actual I/O, you cna either roll your own and
e.g. use Coro::AnyEvent::readable/writable, or you can use AnyEvent::Handle
with rouse functions:

   $hdl = new AnyEvent::Handle ...;

   $hdl->push_read (line => Coro::rouse_cb);
   my (undef, $line) = Coro::rouse_wait;

Or you can use Coro::Handle, which is most convenient, but comes with some

   $fh = Coro::Handle::unblock $fh;

   my $line = <$fh>;
   syswrite $fh, "oipsy";

That should give you some ideas.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_    
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at
      -=====/_/_//_/\_,_/ /_/\_\

More information about the anyevent mailing list