Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Perl6: react/whenever timeout

by moritz (Cardinal)
on Jul 21, 2016 at 20:57 UTC ( #1168273=note: print w/replies, xml ) Need Help??


in reply to Perl6: react/whenever timeout

Concurrency is not my big strength, so this will likely turn out to be a bit more clunky than necessary.

One possible trick is to use the Promise.anyof combinator to get the first of the timeout or the actual receiving action:

my $c = channel("Message"); say "Listening"; loop { my $timeout = Promise.in(1.75); my $result = start { $c.receive } my $combined = Promise.anyof( $result , $timeout); await $combined.then({ if $result { say $result.result; } else { say 'timeout'; } }) }

This puts a timeout on every individual .receive, but finishes early if a value is available earlier. It's also not a busy loop, so doesn't use much CPU.

I'm sure there are much more elegant solutions out there if you use a supply instead of a channel to generate the values; maybe some of the promise combinators like zip-latest can be used then.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1168273]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2020-06-02 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (13 votes). Check out past polls.

    Notices?