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.
-
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.
|