[英]If multiple subscribers block on a core.async channel, is the order in which they take the values out when they come guaranteed?
Given an unbuffered core.async
channel (or buffered, it shouldn't really matter), if multiple subscribers all wait on the channel, what is the order in which they take out the incoming messages? 给定一个无缓冲的
core.async
通道(或缓冲的,它实际上并不重要),如果多个订阅者都在该通道上等待,则他们取出传入消息的顺序是什么? For example 例如
(take! ch #(println "callback " 1 " got value " %))
(Thread/sleep 1000)
(take! ch #(println "callback " 2 " got value " %))
(Thread/sleep 1000)
(take! ch #(println "callback " 3 " got value " %))
(Thread/sleep 1000)
(put! ch 'a)
(put! ch 'b)
(put! ch 'c)
It seems that the order in this case is always 1 a
, 2 b
, 3 c
, but I'm not sure if this is guaranteed, or if I'm just getting lucky? 在这种情况下,顺序似乎始终是
1 a
, 2 b
, 3 c
,但是我不确定这是否可以保证,或者我是否很幸运? The Thread/sleep
is added only to make sure that the subscribers subscribe in the order 1 2 3
. 添加
Thread/sleep
仅是为了确保订阅者以1 2 3
的顺序订阅。
If the ordering is guaranteed in this case, are there any other cases with core.async
, where the order of putting/taking things out of a channel is random? 如果在这种情况下可以保证排序,那么是否有其他情况与
core.async
一起core.async
,从通道放入/取出物品的顺序是随机的吗? I remember hearing somewhere, that some part of core.async
intentionally introduces randomess to avoid users depending on some ordering of events, but I'm not sure if this was in relation to put!
我记得在某处听说过,
core.async
某些部分有意引入了randomess来避免用户根据事件的某些顺序来进行排序,但是我不确定这是否与put!
and take!
take!
. 。
The reasony why I'm asking this is because I wanted to use core.async
as a synchronization mechanism, since it's rather simple to use. 我之所以这样问,是因为我想使用
core.async
作为同步机制,因为它使用起来相当简单。 But in order to do that, I need to know if it'd actually synchronize anything, or introduce more randomness and non-determinism. 但是为了做到这一点,我需要知道它是否实际上可以进行任何同步,或者引入更多的随机性和不确定性。
The docstrings to take!
要
take!
的文档字符串take!
and put!
put!
make it explicitly clear that these are asynchronous operations, hence the API does not promise any order. 明确指出这些是异步操作,因此API不承诺任何顺序。
You have to either pass continuations to take!
您必须通过延续才能
take!
and put!
put!
or use the synchronous equivalents <!!
或使用同步等效项
<!!
/ >!!
/
>!!
( <!
/ >!
in a go
block). (在
go
块中为<!
/ >!
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.