concurrent.futures和asyncio.futures有什么区别?

2024-04-25 21:26:16 发布

您现在位置:Python中文网/ 问答频道 /正文

要澄清此问题的原因:

  1. 使用同一名称的两个模块是令人困惑的。它们代表什么使它们与众不同?

  2. 一个能解决另一个不能解决的任务,反之亦然?


Tags: 模块名称原因代表
2条回答

^{} 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.

基本上,如果您正在使用ThreadPoolExecutorProcessPoolExecutor,或者希望直接使用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;

相关问题 更多 >