#perl use AnyEvent; my $input_waiting = AnyEvent->condvar; my $output_waiting = AnyEvent->condvar; my $input_from_stdin = AnyEvent->io ( fh => \*STDIN, # which file handle to check poll => "r", # which event to wait for ("r"ead data) cb => sub { # what callback to execute my $input = ; # read it push(@input, $input); $input_waiting->send; $output_waiting->recv; print shift(@output) ."\n"; } ); while(1){ # this "blocks" (while handling events) till the callback # calls ->send $input_waiting->recv; ## do some possibly lengthy processing push(@output, 'done'); $output_waiting->send; ## finish this cycle, ## AnyEvent docs say: "You can only wait once on a condition - additional calls are valid but will return immediately." ## but we need to stop this loop again, so this: $input_waiting = AnyEvent->condvar; }