develooper Front page | perl.perl6.users | Postings from October 2017

Re: Nice Channel and thread example

Thread Previous
From:
Elizabeth Mattijsen
Date:
October 30, 2017 23:46
Subject:
Re: Nice Channel and thread example
Message ID:
42A5373A-8D34-4BC8-811A-9DD847B99600@dijkmat.nl
This also happens on HEAD, investigating...

> On 31 Oct 2017, at 00:32, Norman Gaywood <ngaywood@une.edu.au> wrote:
> 
> Hmm, in developing this I was using an old perl6:
> 
> This is rakudo version 2015.11-554-g3b4964b built on MoarVM version 2015.11-49-g27f15d9 implementing Perl v6.b.
> 
> I updated perl6 to the one that comes with Fedora:
> 
> This is Rakudo version 2017.08 built on MoarVM version 2017.08.1 implementing Perl 6.c
> 
> Now when I run this program I get an exception when I do:
> 
> close $dir-channel;
> 
> Tried to get the result of a broken Promise
>   in block <unit> at ./traverse-dir0.p6 line 7
> 
> Original exception:
>     Cannot resolve caller close(Channel); none of these signatures match:
>         (IO::Handle:D $fh)
>       in block  at ./traverse-dir0.p6 line 12
> 
> However, if I change the line 12 to:
> 
> $dir-channel.close;
> 
> The program does not throw and exception on this line and runs properly.
> 
> 
> 
> On 30 October 2017 at 16:58, Norman Gaywood <ngaywood@une.edu.au> wrote:
> Looking at Andrew Shitov's new "Using Perl6" book and was playing around with his "94. Parallel file processing" and came up with the following (I think nice) example.
> 
> # There is a thread to populate $dir-channel by reading filenames in a directory with dir()
> # and $N worker threads to read the filenames from the $dir-channel.
> 
> use v6;
> 
> sub MAIN( $dir = ".", :$N = 4 ) {
> 
>     my $dir-channel = Channel.new();
>     my $dir-read = start {
>         $dir-channel.send( $_ ) for dir $dir;
>         close $dir-channel;
>     }
> 
>     my @workers = (^$N).map: {
>         start {
>             while my $file = $dir-channel.receive() {
>                 say $file.path;
>             }
>             CATCH {
>                 when X::Channel::ReceiveOnClosed { .resume }
>             }
>         }
>     }
> 
>     await $dir-read, @workers;
> }
> 
> 
> -- 
> Norman Gaywood, Computer Systems Officer
> School of Science and Technology
> University of New England
> Armidale NSW 2351, Australia
> 
> ngaywood@une.edu.au  http://turing.une.edu.au/~ngaywood
> Phone: +61 (0)2 6773 2412  Mobile: +61 (0)4 7862 0062
> 
> Please avoid sending me Word or Power Point attachments.
> See http://www.gnu.org/philosophy/no-word-attachments.html
> 
> 
> 
> -- 
> Norman Gaywood, Computer Systems Officer
> School of Science and Technology
> University of New England
> Armidale NSW 2351, Australia
> 
> ngaywood@une.edu.au  http://turing.une.edu.au/~ngaywood
> Phone: +61 (0)2 6773 2412  Mobile: +61 (0)4 7862 0062
> 
> Please avoid sending me Word or Power Point attachments.
> See http://www.gnu.org/philosophy/no-word-attachments.html

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About