编程语言
首页 > 编程语言> > python – concurrent.futures和asyncio.futures有什么区别?

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

作者:互联网

澄清这个问题的原因:

>使用两个名称相同的模块令人困惑.它们代表什么使它们与众不同?
>什么任务可以解决另一个不能,反之亦然?

解决方法:

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.

标签:concurrent-futures,python,python-3-x,module,python-asyncio
来源: https://codeday.me/bug/20191004/1853256.html