Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-04-19 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found