concurrent.futures 和 asyncio.futures 有什么区别?
What is the difference between concurrent.futures and asyncio.futures?
澄清一下这个问题的原因:
使用同名的两个模块会造成混淆。它们代表什么使它们与众不同?
哪些任务一个人可以解决而另一个人不能,反之亦然?
来自docs:
[asyncio
provides a] Future class that mimics the one in the concurrent.futures module, but adapted for use with the event loop;
asyncio
documentation涵盖了不同之处:
class asyncio.Future(*, loop=None)
This class is almost compatible with concurrent.futures.Future
.
Differences:
result()
and exception()
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’s call_soon_threadsafe()
.
- This class is not compatible with the
wait()
and as_completed()
functions in the concurrent.futures
package.
This class is not thread safe.
基本上,如果您正在使用 ThreadPoolExecutor
或 ProcessPoolExecutor
,或者想直接使用 Future
进行基于线程或基于进程的并发,请使用 concurrent.futures.Future
.如果您使用 asyncio
,请使用 asyncio.Future
。
澄清一下这个问题的原因:
使用同名的两个模块会造成混淆。它们代表什么使它们与众不同?
哪些任务一个人可以解决而另一个人不能,反之亦然?
来自docs:
[
asyncio
provides a] Future class that mimics the one in the concurrent.futures module, but adapted for use with the event loop;
asyncio
documentation涵盖了不同之处:
class
asyncio.Future(*, loop=None)
This class is almost compatible with
concurrent.futures.Future
.Differences:
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.This class is not thread safe.
基本上,如果您正在使用 ThreadPoolExecutor
或 ProcessPoolExecutor
,或者想直接使用 Future
进行基于线程或基于进程的并发,请使用 concurrent.futures.Future
.如果您使用 asyncio
,请使用 asyncio.Future
。