Marc Lehmann > Coro-5.2 > Coro::AnyEvent

Download:
Coro-5.2.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 5.2   Source   Latest Release: Coro-5.21

NAME ^

Coro::AnyEvent - integrate threads into AnyEvent

SYNOPSIS ^

 use Coro;
 use Coro::AnyEvent;

 # use coro within an AnyEvent environment

DESCRIPTION ^

When one naively starts to use threads in Perl, one will quickly run into the problem that threads that block on a syscall (sleeping, reading from a socket etc.) will block all threads.

If one then uses an event loop, the problem is that the event loop has no knowledge of threads and will not run them before it polls for new events, again blocking the whole process.

This module integrates threads into any event loop supported by AnyEvent, combining event-based programming with coroutine-based programming in a natural way.

All you have to do is use Coro::AnyEvent, run the event loop of your choice in some thread and then you can run threads freely.

USAGE ^

This module autodetects the event loop used (by relying on AnyEvent) and will either automatically defer to the high-performance Coro::EV or Coro::Event modules, or will use a generic integration into any event loop supported by AnyEvent.

Note that if you need to wait for a single event, the rouse functions will come in handy (see the Coro manpage for details):

   # wait for single SIGINT
   {
      my $int_w = AnyEvent->signal (signal => "INT", cb => Coro::rouse_cb);
      Coro::rouse_wait;
   }

FUNCTIONS ^

Coro::AnyEvent offers a few functions that might be useful.

Coro::AnyEvent::poll

This call will block the current thread until the event loop has polled for new events and instructs the event loop to poll for new events once, without blocking.

Note that this call will not actually execute the poll, just block until new events have been polled, so other threads will have a chance to run.

This is useful when you have a thread that does some computations, but you still want to poll for new events from time to time. Simply call poll from time to time:

   my $long_calc = async {
      for (1..10000) {
         Coro::AnyEvent::poll:
         # do some stuff, make sure it takes at least 0.001s or so
      }
   }

Although you should also consider idle or idle_upto in such cases.

Coro::AnyEvent::sleep $seconds

This blocks the current thread for at least the given number of seconds.

Coro::AnyEvent::idle

This call is similar to poll in that it will also poll for events. Unlike poll, it will only resume the thread once there are no events to handle anymore, i.e. when the process is otherwise idle.

Coro::AnyEvent::idle_upto $seconds

Like idle, but with a maximum waiting time.

If your process is busy handling events, calling idle can mean that your thread will never be resumed. To avoid this, you can use idle_upto and specify a timeout, after which your thread will be resumed even if the process is completely busy.

Coro::AnyEvent::readable $fh_or_fileno[, $timeout]
Coro::AnyEvent::writable $fh_or_fileno[, $timeout]

Blocks the current thread until the given file handle (or file descriptor) becomes readable (or writable), or the given timeout has elapsed, whichever happens first. No timeout counts as infinite timeout.

Returns true when the file handle became ready, false when a timeout occured.

Note that these functions are quite inefficient as compared to using a single watcher (they recreate watchers on every invocation) or compared to using Coro::Handle.

Note also that they only work for sources that have reasonable non-blocking behaviour (e.g. not files).

Example: wait until STDIN becomes readable, then quit the program.

   use Coro::AnyEvent;
   print "press enter to quit...\n";
   Coro::AnyEvent::readable *STDIN;
   exit 0;

IMPLEMENTATION DETAILS ^

Unfortunately, few event loops (basically only EV and Event) support the kind of integration required for smooth operations well, and consequently, AnyEvent cannot completely offer the functionality required by this module, so we need to improvise.

Here is what this module does when it has to work with other event loops:

SEE ALSO ^

AnyEvent, to see which event loops are supported, Coro::EV and Coro::Event for more efficient and more correct solutions (they will be used automatically if applicable).

AUTHOR ^

 Marc Lehmann <schmorp@schmorp.de>
 http://home.schmorp.de/