Use of condvar in a callback of another event

Marc Lehmann schmorp at schmorp.de
Sun Mar 17 18:17:23 CET 2013


On Sun, Mar 17, 2013 at 07:28:42PM +0800, Michael Fung <mike at 3open.org> wrote:
> this sub will be called in a callback of another event. Will this
> "nesting" have undesirable effects during high load?

It might have undesirable effects under any load, which is why AnyEvent
tries to catch it if it can.

If you have trouble with the "don't call us, we call you" of event
programming, you could try mixing it with Coro for the difficult parts, e.g.

   use Coro;

   async { # start new thread
      my $redis = ...;
      $redis->connect (...);

      $redis->hmget (..., Coro::rouse_cb);
      my @results = Coro::rouse_wait;
   };

   EV::loop;

That also works with AnyEvent condvars:

   async { # start new thread
      $redis->hmget (..., my $cv = AE::cv);
      my @results = $cv->recv;
   };

While you still can't block in an event loop callback with Coro, you can
either do your stuff in another thread, running concurrently with the
event loop, or start a thread from within an event loop callback.

Coro automatically augments AnyEvent's condvars so that they can be used
concurrently.

> # $redis is an EV::Hiredis object already created

Didn't know there was an EV::Hiredis module, interesting.

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\



More information about the anyevent mailing list