Home > Perl6 > Out of Order Mystery

Out of Order Mystery

Judging by our irc logs Proc::Async seams not to be well documentent. In the following I shall procide some answers and hopefully raise the missing questions so we can fill another few holes in the docs.

First some code with line numbers.

      1 #! /usr/bin/env perl6
      2
      3 use v6.c;
      4
      5 my $line-source = Proc::Async.new("perl6", "bin/line-source.p6", :w);
      6
      7
      8 react {
      9     whenever $line-source.stdout -> $l is copy {
     10         $line-source.put: $l++;
     11         # die "ouch!" if $++ > 100;
     12     }
     13     whenever $line-source.start {
     14         say "exitcode: ", .exitcode; done
     15     }
     16     whenever signal(SIGINT) {
     17         say "sigint: $_";
     18         $line-source.close-stdin;
     19         exit 0
     20     }
     21     whenever Promise.in(5) {
     22         note "timeout";
     23         $line-source.kill;
     24     }
     25     whenever Promise.in(2) { $line-source.put: 1 }
     26
     27     CONTROL { say "control: $_"; }
     28     CATCH { say .^name, ': ', .Str; }
     29     LEAVE { say "LEAVE"; }
     30     CLOSE { say "CLOSE"; }
     31 }

And now the order of execution of those lines.

Any line with a whenever statement is executed in the order given but the whenever block itself is delayed. Think of it as a setup phase.

The first line executed in the react block is the leave block in line 29. The whole point of a react block is to add one or many invisible awaits that take over control flow inside the block. Any exception fired inside any whatever and the surrounding react block will be handled in the CATCH block of line 28. If any exception is forcing any Supply to be closed, the CLOSE block will be run. Normal execution wont (this may be a bug).

If no odd things happend, line 10 is run whenever a newline shows up in STDOUT. The signal handler at line 17 is fired when the signal is processed (that can take a bit). The timeout of 5 seconds may take more then 5 seconds but it will close STDIN and as such force line 14 to be executed. If the program started in line 13 exits on its own, line 14 will be run as well.

The block in line 25 is executed after at least 2 seconds after setup.

There are likely bits I missed. Your questions are very welcome. I for one need rest now to get my brain back in order.

UPDATE: There is no timeout for reads from buffered programs. So if they don’t have a timeout on their own, they may hang until STDIN is closed.

Advertisements
Categories: Perl6
  1. No comments yet.
  1. August 29, 2017 at 01:37

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: