parallel.futures和asyncio.futures有什么区别? [英] What is the difference between concurrent.futures and asyncio.futures?
问题描述
要阐明此问题的原因:
-
使用两个具有相同名称的模块会造成混淆.它们代表什么使它们与众不同?
It is confusing to use two modules with the same name. What do they represent that makes them distinct?
一个任务可以解决另一个任务不能解决的问题,反之亦然?
What task(s) can one solve that the other can't and vice-versa?
推荐答案
asyncio
文档涵盖了差异:
The asyncio
documentation covers the differences:
类
asyncio.Future(*, loop=None)
此类几乎与concurrent.futures.Future
兼容.
差异:
-
result()
和exception()
不会接受超时参数,并且在未来还没有结束时会引发异常. - 在
add_done_callback()
中注册的回调总是通过事件循环的call_soon_threadsafe()
调用. - 此类与
concurrent.futures
程序包中的wait()
和as_completed()
函数不兼容.
result()
andexception()
do not take a timeout argument and raise an exception when the future isn’t done yet.- Callbacks registered with
add_done_callback()
are always called via the event loop’scall_soon_threadsafe()
. - This class is not compatible with the
wait()
andas_completed()
functions in theconcurrent.futures
package.
此类不是线程安全的.
基本上,如果您使用的是ThreadPoolExecutor
或ProcessPoolExecutor
,或者想直接将Future
用于基于线程或基于进程的并发,请使用concurrent.futures.Future
.如果您使用的是asyncio
,请使用asyncio.Future
.
Basically, if you're using ThreadPoolExecutor
or ProcessPoolExecutor
, or want to use a Future
directly for thread-based or process-based concurrency, use concurrent.futures.Future
. If you're using asyncio
, use asyncio.Future
.
这篇关于parallel.futures和asyncio.futures有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!