试图理解为什么创建和操纵期货是一种不好的做法 [英] Trying to undestand why creating and manipulating futures a bad practice

查看:66
本文介绍了试图理解为什么创建和操纵期货是一种不好的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过阅读 PyMotW 指南来理解Python中的期货.

I'm trying to understand futures in Python by reading the PyMotW guide.

昨天我问了一个问题,并在评论中指出:

I asked a question yesterday, and in the comments, it was stated that:

该指南的一个更严重的问题是创建和操作 在现代异步编程中,原始期货被认为是不良样式.

A more serious issue with the guide is that creating and manipulating raw futures is considered bad style in modern async programming.

我尝试搜索更多信息,但是找到的最接近的是

I tried searching for more information, but the closest I found was in the Python guide about Future:

经验法则是从不以面向用户的方式公开Future对象 API,建议的创建Future对象的方法是调用 loop.create_future().这样,备用事件循环实现 可以注入自己对Future对象的优化实现.

The rule of thumb is to never expose Future objects in user-facing APIs, and the recommended way to create a Future object is to call loop.create_future(). This way alternative event loop implementations can inject their own optimized implementations of a Future object.

问题:

为什么创建和操纵期货被认为是不好的做法?原因记录在某处吗?

Why is it considered bad practice to create and manipulate futures? Are the reasons documented somewhere?

推荐答案

感谢@ user4815162342,它提供了到async/await的链接演示文稿.

Thanks to @user4815162342, for providing a link to the async/await presentation by Yury Selivanov.

我设法伸出手去问他.这是他说的话.

I managed to reach out and ask him. Here is what he said.

出现警告是因为Future是一个低级API.你应该 使用它围绕现有的基于回调的对象构建异步/等待外观 代码.但是,如果您是从头开始编写异步/等待代码,则有 在asyncio中更高级的内置async/await API,更加简单 使用(而且更不容易出错).

The warning is there because Future is a low-level API. You should use it to build async/await facade around existing callbacks-based code. But if you are writing async/await code from scratch, there are higher-level builtin async/await APIs in asyncio that are just easier to use (and less error prone).

这篇关于试图理解为什么创建和操纵期货是一种不好的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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