parallel.futures和asyncio.futures有什么区别? [英] What is the difference between concurrent.futures and asyncio.futures?

查看:94
本文介绍了parallel.futures和asyncio.futures有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要阐明此问题的原因:

  1. 使用两个具有相同名称的模块会造成混淆.它们代表什么使它们与众不同?

  1. 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() 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.

此类不是线程安全的.

基本上,如果您使用的是ThreadPoolExecutorProcessPoolExecutor,或者想直接将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆