event grouping and Coro

Marc Lehmann schmorp at schmorp.de
Tue Feb 28 19:48:08 CET 2012


On Tue, Feb 28, 2012 at 09:56:55AM -0800, gleeco <gleeco at gmail.com> wrote:
> i'm wondering what the best way to handle logical grouping in Coro for

I am not sure there is a canonical "best" way.

> Coming from AE, here's a typical use case:
> 
> I would like to employ this grouping in a single Coro thread.

Well, your example should work as-is in a single Coro thread - waiting on
an AnyEvent condvar blocksonly the calling thread, not the whole process.

And since http_get *is* event-based, it's probably also simplest to use a
condvar.

The comparable pattern in Coro would be to create threads and join them
(if your operations are not event-based):

   my @thr;
   
   for (1, 2, 3, 4) {
      push @thr, async { ... };
   }

   $_->join
      for @_;

Or simply:

   my @res = map $_->join,
             map +(async { ... }),
                 1, 2, 3, 4;

A near-equivalent to the counting behaviour (begin/end) would be a counting
semaphore:

   my $sem = new Coro::Semaphore;

   for (1, 2, 3, 4) {
      $sem->adjust (-1);
      async {
         ...
         $sem->up;
      };
   }

   $sem->down;

> I understand rouse_cb, but in this case, it doesn't seem to make sense
> where there are
> >1 events per thread that i'm waiting on.

rouse_cb could be used just as well, as the callback for the condvar:

  my $cv = &AE::cv (Coro::rouse_cb);

  $cv->begin;
  ...
  $cv->end;

  Coro::rouse_wait;

but $cv->recv should be more easier to read.

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