As an exercise in enlightenment, I've been going through Rob Pike's "Go Concurrency Patterns" video, trying to rewrite his examples in Perl 6.

Where I'm having trouble, though, is getting Channel.receive to timeout per request. (I can easily make a global timeout for all communication, but I want the timeout to reset after each message comes through.)

The following works, but I feel like I should be using something "react/whenever" or something, to make it more idiomatic.

PS — Yes, I know, "There's More Than One Way To Do It" (and "If It's Stupid, But It WORKS, It Isn't Stupid!") If this were an actual application, I'd be happy to leave it "as is"; I'm just trying to understand the "More Than One" ways... «grin»

#!/usr/bin/env perl6 sub channel (Str $msg --> Channel) { my $c = Channel.new; start { for ^Inf -> $i { my $rand = (^2e3).pick / 1000; $c.send( "$msg $i = $rand ms" ); sleep $rand; # 0–2 seconds } } return $c; } my $c = channel("Message"); say "Listening"; my $timeout = Promise.in(1.75); loop { if $c.poll -> $item { say qq|Got: "$item"|; # Reset Timeout $timeout = Promise.in(1.75); # Exit if channel takes > 1.75 seconds } elsif $timeout { say "Timeout"; last } } say "Finished";
However, if I change that to:
react { whenever $c -> $item { say qq|Got: "$item"| } whenever Promise.in(5) { say "Timeout"; done } }
... it always runs for ~five seconds, and then exits. (You'd think it would never timeout, since the channel is set to send at least one message every two seconds.)

Any idea what I'm missing?

[Edit: Refactored "timeout" channel with Promise.in()]


In reply to Perl6: react/whenever timeout by OneTrueDabe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.