AnyEvent->loop?

Marc Lehmann schmorp at schmorp.de
Wed Feb 16 03:01:22 CET 2011


On Wed, Feb 16, 2011 at 12:16:27AM +0000, Steve Freegard <steve.freegard at fsl.com> wrote:
> I have one API related question though; when AnyEvent->condvar->wait
> is used for mainloop emulation so it's not possible to use another
> condvar elsewhere without receiving the 'AnyEvent::CondVar:
> recursive blocking wait detected at ..' error.

It is, as long as it is not blocking, or you use Coro threads (and wait in
a separate thread).

Note also that ->wait is an unsupported and undocumented method, you
should use the documented ->recv instead.

> I realised that I should really be calling the actual event loop
> providers event loop instead in my main program and then my CondVar's
> worked as expected.

One would argue that they work as expected already, see below.

> Looking at the actual code and not the documentation - it would seem
> that I can simply call:
> 
> AnyEvent->loop;

While that might work in some cases, it does not work with all backends.

> Any reason why this isn't documented?  I've tried it and it works great.

Well, it certainly doesn't work great when the method isn't available in a
backend, so "works great" is of limited value.

The reason it is not documented is that AnyEvent is primarily meant for
module authors and not standalone programs (where you can usually just use
whatever event loop you want directlry).

This is not entirely true anymore - AnyEvent apparently has become popular
for (some) standalone programs, but still I couldn't bring it over me to make
it so easy to block.

Now, your concrete issue seems to be different: you want to use
AE::cv->recv as a main loop replacement and than run into the problem of
not being able to block via a condvar in a callback.

While you can work aroudn this protection by running an event loop, note that
you already have a semantic bug and likely an actual bug - the semantic bug
is that you are not supposed to block the process in some callback, i.e. by
using a blocking wait on a condvar in a callback, your program stopped beign
event-based.

The likely actual bug is that you *probably* don't protect against recursion,
i.e. what keeps your blocking callback from being invoked again recursively?
Or what happens when there is another blocking wait call somewhere else
(maybe not even in your code?) - what keeps that from recursing?

So using AE::cv->recv as "mainloop" protects you from these problems.

-- 
                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