5

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:

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.

Question:

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

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • 2
    There's absolutely **nothing** wrong in creating and manipulating futures. It is just a low level object tied to an underlying event loop. So extra care is needed. That's all. Also note that I've spent lots of time with asyncio and I probably had no more then 10 situations when I had to work with futures directly. Most of your problems can be solved without them (typically you wrap "callback style" functions with futures). But that doesn't make them a bad practice. – freakish Jan 21 '19 at 10:52
  • @freakish For the record, I agree with your comment 100%, and I wrote the answer the OP is referring to. The answer was written in context of a beginner text which introduced asyncio to beginners through `call_soon` and explicitly passing future objects to callbacks in places where a simple coroutine would be much more appropriate. As you wrote, futures are low-level objects and there is a time and place for them. – user4815162342 Jan 22 '19 at 16:53
  • @EmilyScott If you're looking for a reference, see e.g. [this presentation](https://speakerdeck.com/1st1/asyncio-today-and-tomorrow?slide=19) by the current maintainer of asyncio, which explicitly warns against (over)use of futures, `call_soon`, transports, protocols, and the event loop machinery. – user4815162342 Jan 22 '19 at 16:54
  • @user4815162342 - The presentation doesn't say why though. –  Jan 22 '19 at 16:59
  • @EmilyScott I'm not the author of the presentation, but I suspect it's because more powerful and less error-prone abstractions are now available, beginning with `await` and streams, and soon hopefully including things like task groups and supervisors. As just an example, when you use `call_soon`, exceptions raised in the callback are simply lost (reported to the event loop) unless you take careful steps to retrieve them. – user4815162342 Jan 22 '19 at 17:57
  • 1
    @user4815162342 - Exactly what you stated in your comment, is what he said. I e-mailed and reached out to him. Below is his answer. –  Jan 22 '19 at 19:16
  • @EmilyScott Did you perhaps manage to find a video of the presentation? It looks like it would be extremely useful, and I was unable to locate it. – user4815162342 Jan 22 '19 at 19:32
  • @user4815162342 - Not yet, I'll do some more digging around and see what i can find. –  Jan 22 '19 at 19:53
  • 1
    I've removed the `concurrent.futures` tag because my comment was completely unrelated to the futures defined by that module. The futures referred to in the quote are the ones defined in asyncio. – user4815162342 Jan 22 '19 at 19:58
  • @user4815162342 - This may go beyond the scope of the question, but I found a link to another article explaining async/await, you could add it to your blog list. https://realpython.com/async-io-python/#async-io-is-not-easy –  Jan 22 '19 at 23:27
  • @EmilyScott Thanks, I've already seen it, but I haven't gotten around to reading it yet (it's only several days old). It seems very promising, and the author has provided interesting questions and answers on the python-asyncio tag here. – user4815162342 Jan 22 '19 at 23:31
  • @user4815162342 - Is there a way someone could keep up with what goes on at PyCon, maybe a site that give a run down of the topics discussed? Given that Yury was nice enough to reach out and answer my question, I would like to keep up with current topics in Python. How did you know about the async presentation? –  Jan 22 '19 at 23:34
  • Found the video: https://www.youtube.com/watch?v=ReXxO_azV-w – user4815162342 Jan 23 '19 at 18:52
  • @user4815162342 - Nice! Terribly sorry about the live chat request.I was busy when I posted my last comment. –  Jan 23 '19 at 21:08

1 Answers1

2

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.

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

user4815162342
  • 141,790
  • 18
  • 296
  • 355