Wrap the creation of your condvars and AE::io objects in your while loop and use callbacks on the condvars instead of procedural code to do the sequential tasks, something like this:
#!/usr/bin/perl use strict; use warnings; use AnyEvent; while (1) { my $cv = AE::cv; my (@output, @input); my $input_waiting = AE::cv; my $output_waiting = AE::cv; my $input_from_stdin = AE::io *STDIN, 0, sub { my $input = <STDIN>; # read it push(@input, $input); $input_waiting->send; }; # what to do when $input_waiting has sent $input_waiting->cb( sub { push(@output, 'done'); $output_waiting->send; }); # what to do when $output_waiting has sent $output_waiting->cb( sub { print shift(@output) ."\n"; $cv->send; }); # wait for the main condvar $cv->recv; }
the while block ensures that new condvars and AE::io objects get created on each iteration of the loop. I used the simpler AE API, but that shouldn't affect the functioning of the code. You could probably get by without the $cv condvar, but it may be helpful as this is part of a larger application. You should look at the begin and end methods of AE::condvar which enables the use of single condvars as mergepoints for events, and Object::Event for a lightweight event registration and emitting library.

In reply to Re: Doing an Input->process->Output cycle with AnyEvent? (was: error: "recursive blocking wait detected") by james2vegas
in thread Doing an Input->process->Output cycle with AnyEvent? (was: error: "recursive blocking wait detected") by isync

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.