New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
receive from channel with timeout #2990
Comments
I think we'll eventually have a Right now you need to do it in an ugly way: https://github.com/crystal-lang/crystal/blob/master/samples/channel_select.cr Of course for timeout you need to spawn, sleep and then send through a channel. @waj wants things other than channels to be able to be used in |
Check https://github.com/crystal-lang/crystal/blob/master/samples/channel_select.cr Eventually I think we'll have a For timeout you need to spawn, sleep and then send through a channel, although @waj wanted this to be simpler, maybe things other than channels could be used in |
what about this? ch = Channel(Nil).new
spawn do
sleep 10.seconds
ch.timeout!
end
begin
x = ch.receive
rescue Channel::Timeout
end |
Exceptions are slow, they shouldn't be used for control flow. |
Timeouts could be implemented in the |
add new method |
It'll be something like this: select
when value = ch.receive
puts value
when sleep(10.seconds)
puts "Timeout!"
end The idea is that this gets rewritten, so a call like |
but what if someone send to channel later, what happen in this case? block forever? |
@kostya I don't know because I don't have much experience with channels and timeouts, but I guess on a timeout you'd close the channel so nothing else can be sent on it... maybe it depends on the specific scenario? Or else, we'd have to think how this is done in Go. |
This is a receive timeout, so it should merely mean: it expected to receive a message within X seconds but it didn't receive one. I think it's up to the developer to decide to close the channel or not, depending on its usage and expectations. |
hm, if i can close the channel so there is no problem. sender should crash in this case. |
this is nice now i use: spawn do
sleep 10
ch.close
end
spawn do
sleep 20
ch.send 1 # => Exception
end
res = ch.receive rescue "timeouted" |
how to do that?
in go i can
The text was updated successfully, but these errors were encountered: