简体   繁体   English

如果多个订阅者在core.async通道上阻塞,那么在保证值时取出值的顺序是什么?

[英]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 a2 b3 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.

相关问题 在core.async中阻止vs线程 - go block vs thread in core.async Future vs Thread:哪个更适合在 core.async 中使用通道? - Future vs Thread: Which is better for working with channels in core.async? 使用Clojure core.async限制进程 - Throttling Processes using Clojure core.async 何时使用非阻塞>! /线程和阻止> !! / goroutines with clojure core.async - When to use non-blocking >! / threads and blocking >!! / goroutines with clojure core.async Clojure core.async,CPU在超时后挂起。 无论如何要正确杀死(go ..)块产生的宏线程? - Clojure core.async, CPU hangs after timeout. Anyway to properly kill macro thread produced by (go..) block? 你怎么杀死core.async / thread? - How do you kill a core.async/thread? 当多个线程添加数据时,可以确保BlockingCollection队列的数据顺序吗? - Is Data order of BlockingCollection queue guaranteed when data is added by multiple threads? 应该在一个clojure core.async线程中放入`while true`吗? - Should one put `while true` inside of a clojure core.async thread? Clojure core.async,有什么方法可以控制(go...)线程池中的线程数? - Clojure core.async, any way to control number of threads in that (go…) thread pool? 当锁定获取顺序无法保证时,避免死锁 - Avoid deadlock when the order of acquisition of locks is not guaranteed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM