简体   繁体   English

我可以在clojure.core.async / go中取消引用期货/承诺吗?

[英]Can I deref futures/promises in clojure.core.async/go?

I'm trying to find a way to harmlessly observe many future s. 我正在尝试找到一种无害地观察许多future的方法。 By that, I mean without blocking N threads to wait for N future s. 这样,我的意思是不阻塞N个线程等待N个future

I see that core.async library is build in a way it doesn't block thread with blocking operations, but rather park it and reuse the thread. 我看到core.async库的构建方式是,它不会通过阻塞操作来阻塞线程,而是将其停放并重新使用线程。 Is it the case with deref, or it works only with <! 是deref的情况,还是仅与<! and alts! alts! ?

the future-done? future-done? function can be used to poll an array of futures to see which ones are ready to be looked at: 函数可用于轮询一系列期货,以查看哪些期货可供查看:

main> (future-done? (future 42))
true
main> (future-done? (future (Thread/sleep 1000) 42))
false

you can then create a function that polls all the futures (in a vector perhaps) for one to work on, then goes and does the work. 然后,您可以创建一个函数来轮询所有期货(可能是矢量)以进行操作,然后继续执行该工作。 this way instead of blocking a thread on each future, you block a thread on any future and decide how many such workers you want (one at a time is a common choice) 这样一来,而不是在每一个未来的阻塞线程,您阻止任何未来的一个线程,并决定你要多少这样的工人(一次一个是共同的选择)

If you want it to be more efficient, or more expressive, than this you would be creating a state machine to track which futures to check and work on which would put you on the path to recreate your own version clojure.core.async/alts! 如果您希望它比以前更有效或更具表现力,则可以创建一个状态机来跟踪要检查和处理的期货,这将使您重新创建自己的版本clojure.core.async/alts! and might do better to use core.async all the way. 并可能更好地始终使用core.async。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM