Skip to Content
Daniel Arbuckle's Mastering Python
book

Daniel Arbuckle's Mastering Python

by Daniel Arbuckle
June 2017
Beginner to intermediate
274 pages
6h 49m
English
Packt Publishing
Content preview from Daniel Arbuckle's Mastering Python

Closing event_loop

A good way to close event_loop is to use the contextlib.closing context manager, which guarantees that the close method will be called once the with block ends. The following code example shows event_loop closing:

Even in error situations, the close method should be called when we're completely done with an event_loop, but this doesn't necessarily mean that it should be called right after the run_forever or run_until_complete call is finished. The event_loop is still in a valid state at that point, and it's perfectly OK to, for example, add some new tasks or start the loop again.

As you may have probably noticed, an asyncio ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On Deep Learning for Games

Hands-On Deep Learning for Games

Micheal Lanham
Mastering PyCharm

Mastering PyCharm

Nafiul Islam
Python: Journey from Novice to Expert

Python: Journey from Novice to Expert

Fabrizio Romano, Dusty Phillips, Rick van Hattem

Publisher Resources

ISBN: 9781787283695Supplemental Content