简体   繁体   English

在core.async中阻止vs线程

[英]go block vs thread in core.async

From http://martintrojer.github.io/clojure/2013/07/07/coreasync-and-blocking-io/ : 来自http://martintrojer.github.io/clojure/2013/07/07/coreasync-and-blocking-io/

To get a bit more concrete let's see what happens when we try to issue some HTTP GET request using core.async. 为了更具体一点,让我们看看当我们尝试使用core.async发出一些HTTP GET请求时会发生什么。 Let's start with the naive solution, using blocking IO via clj-http. 让我们从天真的解决方案开始,通过clj-http使用阻塞IO。

 (defn blocking-get [url] (clj-http.client/get url)) (time (def data (let [c (chan) res (atom [])] ;; fetch em all (doseq [i (range 10 100)] (go (>! c (blocking-get (format "http://fssnip.net/%d" i))))) ;; gather results (doseq [_ (range 10 100)] (swap! res conj (<!! c))) @res ))) 

Here we're trying to fetch 90 code snippets (in parallel) using go blocks (and blocking IO). 在这里,我们尝试使用go块(并阻塞IO)获取90个代码片段(并行)。 This took a long time, and that's because the go block threads are "hogged" by the long running IO operations. 这需要很长时间,这是因为go块线程被长时间运行的IO操作“阻塞”了。 The situation can be improved by switching the go blocks to normal threads. 通过将go块切换到普通线程可以改善这种情况。

 (time (def data-thread (let [c (chan) res (atom [])] ;; fetch em all (doseq [i (range 10 100)] (thread (>!! c (blocking-get (format "http://fssnip.net/%d" i))))) ;; gather results (doseq [_ (range 10 100)] (swap! res conj (<!! c))) @res ))) 

What does it mean that "go block threads are hogged by the long running IO operations"? “长时间运行的IO操作会阻塞块线程”是什么意思?

Go blocks are intended to be a sort of light-weight cooperative threads; Go块旨在成为一种轻量级的协作线程; they provide thread-like behaviour with less overhead than full JVM threads by using a few threads in a pool and switching go blocks when they park - for instance, when waiting on a channel using <! 它们提供类似线程的行为,比完整的JVM线程更少开销,方法是使用池中的几个线程并在停放时切换块 - 例如,在使用<!等待通道时 . The thread-switching cannot work when you call a method in the block that blocks the JVM thread, so you quickly run out of JVM threads. 当您在阻塞JVM线程的块中调用方法时,线程切换不起作用,因此您很快就会耗尽JVM线程。 Most standard Java (and Clojure) IO operations will block the current thread when waiting. 大多数标准Java(和Clojure)IO操作将在等待时阻止当前线程。

What does it mean that "go block threads are hogged by the long running IO operations"? “长时间运行的IO操作会阻塞块线程”是什么意思?

There are a limited number of threads dedicated to serving go blocks*. 有限数量的线程专门用于服务块*。 If you perform a blocking I/O operation on one of those threads, then it cannot be used for any other purpose until that operation completes (unless the thread is interrupted). 如果对其中一个线程执行阻塞I / O操作,则在该操作完成之前不能将其用于任何其他目的(除非线程被中断)。 This is also true for non-go block threads (ie, threads that are returned from the thread function), but non-go block threads do not come from the limited go block thread pool. 对于非去块线程(即,从thread函数返回的thread )也是如此,但是非去块线程不是来自有限的去块线程池。 So if you do blocking I/O in a go block, you are "hogging" that go block's thread from being used by other go blocks, even though the thread isn't doing any actual work (it's just waiting for the I/O operation). 因此,如果你在go块中阻塞I / O,那么即使线程没有做任何实际的工作(它只是在等待I / O),你正在“阻塞”块的线程被其他go块使用操作)。

* That number currently happens to be 42 + the number of processors available to the JVM. *该数字目前恰好是42 + JVM可用的处理器数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Future vs Thread:哪个更适合在 core.async 中使用通道? - Future vs Thread: Which is better for working with channels in core.async? Clojure core.async,CPU在超时后挂起。 无论如何要正确杀死(go ..)块产生的宏线程? - Clojure core.async, CPU hangs after timeout. Anyway to properly kill macro thread produced by (go..) block? Clojure core.async,有什么方法可以控制(go...)线程池中的线程数? - Clojure core.async, any way to control number of threads in that (go…) thread pool? 你怎么杀死core.async / thread? - How do you kill a core.async/thread? 如果多个订阅者在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? 应该在一个clojure core.async线程中放入`while true`吗? - Should one put `while true` inside of a clojure core.async thread? 使用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 线程vs begininvoke vs async - Thread vs begininvoke vs async 在没有睡眠的情况下使用sleep vs block阻塞线程 - Block a thread with sleep vs block without sleep
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM