isync has asked for the wisdom of the Perl Monks concerning the following question:
As you can see, I am trying to implement a simple Input->process->Output mechanism, like for example chatbots use it. Where the printing of the output should be delayed as long as the processing takes. Is my overall design design okay for that?#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 = <STDIN>; # 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 - add +itional calls are valid but will return immediately." ## but we need to stop this loop again, so this: $input_waiting = AnyEvent->condvar; }
|
---|