Advertisement
Guest User

Perl6 code for testingchannel

a guest
Sep 11th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 3.03 KB | None | 0 0
  1. #!/usr/bin/env perl6
  2. ########################################################################
  3. # housekeeping
  4. ########################################################################
  5.  
  6. use v6.d;
  7.  
  8. ########################################################################
  9. # global values
  10. ########################################################################
  11.  
  12. constant  WRITER  = </bin/gzip -9v>;
  13. constant  WRITERS = 4;
  14. constant  OUTDIR  = './out';
  15. constant  NAME    = $*PROGRAM.basename;
  16.  
  17. ########################################################################
  18. # utility subs
  19. ########################################################################
  20.  
  21. sub out-dir
  22. (
  23.   IO()  $dir  = './out'
  24.   --> IO
  25. )
  26. {
  27.   my $out = $dir.IO.absolute.IO;
  28.  
  29.   $out  ~~ :d
  30.   or
  31.   mkdir $out, 0o770
  32.   or
  33.   die "Failed mkdir: '$out', $!"
  34.   ;
  35.  
  36.   note "# Out:  '$out'";
  37.  
  38.   $out
  39. }
  40.  
  41. sub out-path
  42. (
  43.     Stringy $name = NAME
  44.   , *@dir-args
  45.   --> IO
  46. )
  47. {
  48.   constant  format = '%s-%02x.out.gz';
  49.  
  50.   my $dir   = out-dir |@dir-args;
  51.  
  52.   my $base  = sprintf format, $name, ++$;
  53.   my $path  = $dir.add( $base );
  54.  
  55.   note "# Base: '$base' ($name)";
  56.  
  57.   $path
  58. }
  59.  
  60. sub out-open
  61. (
  62.   Stringy $enc = 'ascii'
  63.   , *@path-args
  64.   --> IO::Handle
  65. )
  66. {
  67.   my $path  = out-path |@path-args;
  68.   my $fh    = open $path, :w, :create, :truncate, :enc( $enc )
  69.   or die "Failed open: '$path', $!";
  70.  
  71.   note "# Open: '$fh'";
  72.  
  73.   $fh
  74. }
  75.  
  76. ########################################################################
  77. # set up a list of writers sharing a channel for input.
  78. ########################################################################
  79.  
  80. sub writers
  81. (
  82.     Channel:D $channel
  83.   , Int()     $count  = 1
  84.   , *@file-args
  85.   --> Array
  86. )
  87. {
  88.   state @writers
  89.   = ( 1 .. $count )
  90.   .map\
  91.   (
  92.     {
  93.       note "# Writer: $_";
  94.  
  95.       my $stdout  = out-open |@file-args;
  96.       my $proc    = Proc::Async.new( :w, :enc('ascii'), WRITER );
  97.  
  98.       $proc.stderr.tap( { note "\nMessage: $^a ($stdout).\n" } );
  99.  
  100.       $proc.bind-stdout( $stdout );
  101.       $proc.start;
  102.  
  103.       Promise.start:
  104.       {
  105.         note "# Running: $_";
  106.  
  107.         my $writes  = 0;
  108.  
  109.         loop
  110.         {
  111.           try
  112.           {
  113.             CATCH
  114.             {
  115.               when X::Channel::ReceiveOnClosed
  116.               {
  117.                 note 'Channel closed: ', $stdout.key;
  118.               }
  119.  
  120.               default
  121.               {
  122.                 note "Error: failed recieve/say, $!";
  123.               }
  124.             }
  125.  
  126.             $proc.say: $channel.receive;
  127.             ++$writes;
  128.           }
  129.         }
  130.  
  131.         note "# Closing: $_ ($writes written).";
  132.  
  133.         $proc.close-stdin;
  134.       }
  135.     }
  136.   )
  137. }
  138.  
  139. with Channel.new -> $channel
  140. {
  141.   my $procs = ( %*ENV<procs>  // 1      ).Int;
  142.   my $rows  = ( %*ENV<rows>   // 1_000  ).Int;
  143.  
  144.   note "$*PROGRAM procs=$procs rows=$rows";
  145.  
  146.   my @writers = writers $channel, $procs
  147.   or die 'Failed starting writers.';
  148.  
  149.   $channel.send( $_ ) for 1 .. $rows;
  150.   $channel.close;
  151.  
  152.   say '# Promise:', await Promise.allof( @writers );
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement