This is the website for an older EuroPython. Looking for the latest EuroPython? Click here!
Skip to main content

Asyncio Evolved: Enhanced Exception Handling with TaskGroup in Python 3.11

Level:
beginner
Room:
terrace 2a
Start:
Duration:
30 minutes

Abstract

With the release of Python 3.11 in October 2022, PEP 654 "Exception Groups and except" was accepted, and asyncio.TaskGroup() was added. This enhancement of exception and cancellation handling has allowed asyncio to evolve more flexibly, addressing the existing issues with asyncio APIs, such as insufficient cancellation and exception handling in asyncio.gather.

In this talk, I would like to discuss the problems of existing asyncio APIs and how the newly introduced asyncio.TaskGroup() solves these issues. Attendees will learn about the improved way of handling exceptions and cancellations using asyncio.TaskGroup(), enabling them to write more efficient and robust asynchronous code with Python 3.11.

TalkSoftware Engineering & Architecture

Description

Description

In Python 3.11, asyncio.TaskGroup() and asyncio timeout context managers were added. Additionally, get_event_loop() was deprecated in 3.10, and loop objects were further obscured, making the already less intimidating asyncio even less scary.

I will discuss the basic usage of asyncio in Python 3.11, which has become simpler and more flexible, focusing on exception and cancellation handling.

I hope that this will be helpful for those who choose asyncio when writing web applications using ASGI frameworks or Python scripts where asynchronous I/O is useful.

By the end of this talk, attendees will be able to:

  • Understand the limitations of the existing asyncio APIs, specifically regarding cancellation and exception handling in asyncio.gather.
  • Learn how Python 3.11's asyncio.TaskGroup() overcomes these limitations and improves exception and cancellation handling.
  • Apply the new asyncio.TaskGroup() features in their asynchronous programming to build more resilient and efficient applications.

Planned Outline

  • Overview of asyncio.taskgroup (5 min)
    • New "except*" syntax
      • PEP 654 Exception Groups and except
  • Capturing concurrent exceptions(10 min)
    • Using asyncio.gather() / asyncio.TaskGroup()
  • Cancelling tasks on exception(10 min)
    • Using asyncio.gather() / asyncio.TaskGroup()
  • Writing "predictable" and "safe" asyncio code(5 min)
    • Introducing the new standard "Hello-ish world" example

The speaker

Junya Fukuda

Junya Fukuda

Develops "LOVOT", a family-like robot that promotes the ability to love, at GROOVE X, Inc. He has spoken at PyCon JP, DjangoCongress JP, and other events. As a community activity, he participates in the management of GeekLab Nagano. Co-author of "Python Practical Recipes (2022 Gijutsu Hyoron Co., Ltd.)". Translation of "Expert Python Programming - Fourth Edition". Likes beer, camping, and asyncio. Twitter account is @JunyaFff.


← Back to schedule