DevDays Europe 2025

Dmitrii Ivashchenko: Taking Asynchronous Python to the Next Level Using Trio

47:24 · 20 May 2025 – 23 May 2025 · YouTube

About this talk

This talk focuses on Trio, a groundbreaking library for asynchronous programming in Python. The speaker explains the concept of structured concurrency, which is key to managing tasks effectively and streamlining code. Various features of Trio are covered, including nurseries for managing multiple concurrent tasks, checkpoints for task transitions, and a unique approach to error handling through exception groups. Additionally, the speaker discusses the integration of Trio with file operations and the recent updates that enhance usability. Comparisons with other asynchronous frameworks such as asyncio are also provided, emphasizing Trio's simplicity and robustness, making it an attractive choice for developers.

Full transcript

[Music] welcome back everyone death days Europe and the third session today and we have an awesome speaker with me Dimitri ivanenko hi how you doing hello everyone Hi hi I'm doing well and you yeah I'm also doing fine thank you so I'm your host for rle and in this session we are going to take python or asynchronous python to be more specific to the next level so

uh tell us what uh is awaiting Us in this awesome session yeah they will compare two libraries Tri andur and we will dve into the trio structured concurrency it's a more unique approach to make your code even better your synchronous code more streamlined and reliable awesome can't wait for it and with that the stage is all yours thanks thank you okay uh so hello everyone once again

I'm M from my games and today we are diving deep into Trio groundbreaking library for as sync programming so it's not just cool it's changes how we handle synchronous operations in Python and we'll break down how it uses a well structured concurrency model to manage tasks effectively and this model simplifies task management making your project more Main tangable and less error prawn and I'll go through several

practice so let's get started um let's into the mechanics of how to operate uh we'll start by exploring the concept of structured concurrency uh Cur store of Tri Design This approach is not only cleans up your code but makes it more uh easy to follow and manage to tune find tun it and we'll examine how to manage us timeouts and cancellations uh next we'll discuss how to

ensure smooth synchronizations of tasks and optimal management of ey operations um we also compare features Rel to highlighting why Trio might be better choice in many scenarios and in addition to core features we will delve into Trio in library it's ideal for environments environments that utilize both systems uh and finally we'll uh discuss the latest updates of Trio so let's dive let's start by by delving uh

deep into core features of Trio and Trio design focuses on Simplicity and robustness and handling current tasks and U this core features provide a solid foundation for building reliable and scale publication so the first concept we will tackle is nurseries and spawning within the trio Nur series and spawning are essential for managing multiple synchronous task effectively this feature SI to tri's design promoting structured way to handle

concurrency they ensure that tasks within a nursery I manag from start to finish in a controlled environment um in Tri using nurseries involves a straightforward syntax with ASN statement first uh you initiate a nursery by calling open Nursery function within the nursery constuct we can easily spawn tasks using nursery. star 2 method and this examine illustrates a basic parent child task relationship management within n the child

tasks run concurrently in efficiency in managing multiple tasks and this setap ensures that all child tasks complete before the Nary exit so considering the synx let's apply it to a practical scenario like managing multiple web requests in this set up each web request is handled as an independent task within a nursary and the nurses start the these tasks almost simultaneously and uh this meth is particularly effective

for application that require High responsiveness it ensures all tasks are completed before the program proceeds so moving on from let's delve into another crucial feature of Tri checkpoints checkpoints are vital for effectively managing transitions between tasks and tri they serve two main purposes uh they check for task installation and enable task switching uh this D functionality ensures that synchronous applications remain responsive and robust and by integrating

point three fost Cooperative multitasking environments and it's has overall system efficiency so checkpoints Ur at every ascor that might block such as for example a trio sleep one in this example each checkpoint give Trio an opportunity to perform essal maintenance task like checking for cancellations or switching also allows to schedule to decide whatever is time to switch tasks yeah to understand this let's exam how to operate

within the life through environment and um consider an application that handles multiple database queries simultaneously using checkpoints uh allows each query to be processed uh concurrently enhancing system trut and uh this concurrent procy minimizes response times and maximizes efficiency across all the application for instance while one query waits for a data another can progress uh making the most available system resources and proper use of checkpoints Ure

is that no single tasks monopolizes the system resources yeah it's promoting equal resource allocation across all the tasks uh moving on let's focus on how do effectively handles s Management and uh in complex applications it's common for multiple tasks to fail simultaneously and uh when this happens multiple exceptions can be thrown at the same time and it's complicating handling and tri uses the exception group to encapsulate

all these exceptions together this model is quite efficient it's allowing Developers for to handle several exceptions in one go so let's delve into this mechanic uh D supports except Syntax for handling exception groups introduced in 3.11 and this feature allows each type of exception within the exception group to be managed separately uh such control is environments where multiple tasks run concurrently and let's explore how this index

can be used to streamline uh error handling in your project um imagine a scenario managing file operations where errors are most likely in such cases trios ER handling capabilities come to the Forefront and manag read and write errors effectively when errors Ur they are group together in an exception group this model enables handling errors from multiple operations in a unified manner okay let's discuss uh timeouts at

PR sing GE a bit timeouts are crucial for controlling how low the code segment can run before it need to be cancelled they implement it using cancel Scopes which provide a clean manageable way to handle timing and this set up ensures that operations are not allowed it to run indefinitely yeah using timeouts actually can prevent task from consuming more time than allocated enhancing efficiency so let's explore

how cancel scope works and how they can be applied uh here's how you can uh practically implement this time out to keep your operations moo and efficient to move on after function is exactly for this purpose is essential for setting timeouts for specific code blocks and if the operation within the block exceed the specified time Trio automatically cancels them this cancellation is executed by raising a canceled

exception and this me is straightforward but it's also very powerful for Main and control over tasks and execution times um imagine you imagine a high traffic P server that must handle a large volume of request efficiently and uh setting strategic timeout to key it can ensure that no single request can slow down the server separations Yeah in our example uh 5c timeout is applied to each request

to maintain high responsiveness and if a request cannot be processed within the time frame the server issues a timeout message so this practice not only improve Cal reliability but also enhances the its responsiveness to user requests right now let's uh pivot to another critical feature uh cancellation cancellation is a key mechanism in that allows explicit stopping of tasks whatever manually or due to timeouts it uses a

well structured approach to effectively handle interruptions in tax execution this feature is Streamlight is integrated in Trio model of concurrent programming and it's enhancing control over task life cycles consolation gives developers comprehensive control over stopping synchronous operation whatever necessary and uh it ensures that system resources are not wasted on tasks that no longer continue so as Z care and how we can Implement consolations within your task

for more precise control uh to utilize this function mve and after to set time out time bound limits on tasks and enabling automatic cancellations here if a task doesn't complete within the set time out TR is a canceled exception as we said before and this exception is crucial as it unwinds the execution stack yeah it's allowing task to be stopped cleanly and safely and developers need to

properly handle the exception to ensure that all resources are correctly cleaned so for a practical example consider uh managing database transactions with strategic use of cancellation uh certain timeouts is crucial for ensuring that uh transactions do not affect application performance in our scenario a DAT quer is configured with 3C timeouts to ensure rapid accuration and if the query takes longer that this allow time the transaction Is

AO automatically cancelled by Trio so implementing such consolations helps maintain the stability and responsiveness of the and moving on let's examine how Trio facilitates task communication and synchronization and um effective signalization is crucial for maintaining all and consistency in as synchronous applications and tri offers robust tools for task synchronization crucial for management Shar State um let's break it down and uh the prime example of synchronization Ando

is the semaphor yeah semaphor they used to manage access and concurrent tasks and some controled manner yeah and by acquiring semor a task gains um exclusive access to certain resources yeah preventing some other conflicts uh this pattern is essential for avoiding RIS conditions and ensuring task executed in right sequence and do s for designed to be fair and allocating access based on the order request are made

and by mastering the patterns you can significantly enhance the robustness of your application so another key pattern in Trio synchronization tool kit is producer consumer model yeah this model uses channels to facilitate move and secure communication between tasks uh producer task sends a message to a channel the message are then received by a consumer task and um channels ensure that data is transferred safely and it's uh

maintaining proper sequence and flow control and uh consumer uh processes each message one by one in direct order ensuring that no data is lost or mishandled yeah and using Channel C helps to maintain the clear separation of conerns and enhan that dat Integrity So speaking of channels let's shift our Focus to another core feature in Trio uh stream handling through are your capability are centered throughout comprehensive

stream API for handling by streams and uh this API simplifies the creation of flexible and robust protocol implementations and by abstracting stream faes supports a wide range of data transport mechanisms and this flexibility allows developers to handle data streams in a uniform and efficient manner so let's D deeper in how these API works for instance setting up secure connections is a some much straightforward with the trios

SSL stream SSL stream and we use an SSL stream wrapped around a basic socket stream to secure Communications uh this setop involves establishing an SSL connection over a standard TCP socket and uh SSL stream simplifies encryption by making the transport layer interchangeable and secure uh true streamlined approach enables developers to adapt existing Network code for secure communic Communications almost effortlessly as see so Drew also offers strong

support for asynchronous file system a enhancing responsiveness and the example Explorer involves reading a file asynchronously using trios path object uh a synchronous file operation prevent uh iot task from blocking the main execution tread and this non blocking approach significantly reduces latency in iio intensive applications so it's uh allows for smoother task and better utilization of resources so um hav seen all these features let's compare Trio

with ASU to understand their differences both Frameworks provide powerful tool for synchronous programming but different their code philosophies and implementations D design emphasizes Simplicity and safety which can be seen in its structured approach to concurrency and since on the the other hand offers a more traditional V driven model that prioritizes flexibility and scale yeah so let's explore how these differences affect quod Clarity error handling and overall

system scalability the first notable differ difference is of course structured concurrency model yeah used by Trio and uh Trio model uh organizer tasks into neries it enhan in codability and row containment the structure ensures that tasks are explicitly scope and managed and it's leading to a safer and more maintainable code in the end uh in contrast s relies on an event Loop and Futures to handle tasks

uh offering greater flexibility but at the cost of complexity and uh these differences can significantly influence how developers approach task management and error handling the structured nature of Trio can reduce bugs and simplify debugging process but uh as flexibility can leverage in highly Dynamic environments so Ando as synchronous tasks are managed within nurseries and it's providing clear and explicit scoping this Arrangement highlights the hierarchal nature of

tar dependencies uh that's a benefit and uh it's making the system Behavior more predictable uh in contrast the since uses an event Loop where task managed more Loosely through Futures and callbacks and U this example shows how each model handles task management including everything from error handling to Resource allocation um let's consider an VB application designed to handle multiple simultaneous request effectively and in Trio each request

is managed as a separate task Within Nursery it's ensuring structured and orderly Tas competition and this me provides a clean life cycle for each task task terminating clearly when no longer needed and in contrast the CIO handles each request as a separate task but uh without the explicit structure provideed nurseries um this difference can affect how resource are managed and how tasks are terminating uh impact on

system performance and reliability maybe so now let's look at how Trio and the handles errors differently uh both Frameworks provide mechanisms for error Management in a synchronous environments but the approach is uniquely yeah Tri integrates error handling seamlessly into its structured concurrency model emphasizing Clarity and predictability of course um this integration ensur errors are managed consistently and logically and it's reducing the chance of unhandled exceptions yeah

a s well robust requires more attention to effectively manage errors due to its nature um let's see how this error handling models affect application scalability and developer overheat um TR uses n to encapsulate tasks and ensure errors are contained within the structured blocks in our example nurser acts as a safeguard it's catching and escalating errors from all child tasks to the parent task and this model allows

errors to be handled predictably enhancing code readability and safety and since so on the other hand requires tasks to be explicitly awaited and errors to be carefully handled Um this can lead to challenges in error management especially if exceptions are not handled properly yeah so we'll examine how this explicit error propagation and Jo simplifies debugging and error tracking as you can see structured error handling in Trio

can reduce the complexity there sometimes um now let's consider a we application that needs robust ER management for database queries for example in Tri the structured model ensures that any errors in data fetching are properly handled within the defined scope and uh this model leads to more cleaner more systematic management making the system maybe much a little bit easier to maintain yeah in contrast since to handles

error on per task basis yeah one by one and uh it's require individual task traken and handle for each POS possible exception and uh this approach is flexible but can complicate error in more maybe more larger more complex systems and uh P structured approach can lead to more maintainable predictable error handling and on the other hand s through method requires a careful set up to avoid potential

ped Falls on error propagation so it's quite a bit important of understanding of the difference can help you design better ER handling strategies in your applications so let's thow the distin design philosophies behind Trio and ainio Trio is designed to simplify synchronous programming through Comprehensive High Level obstructions and um it's POS is to make a synon quot as straightforward and error free as it possible uh as

s conversly offers a wide spectrum of apis ranging from low level to high level and uh this variety provides a great flexibility but introduces some complexity and maybe a bit longer loading curve yeah um this difference different philosophies impact usability Main M and scalability of your projects so Tri API aims to get rid of many lowlevel details to streamline the development process and provide a true example

the nursary context manager handles task concurrently with minimal overhead and this approach reduces boiler plate code and focuses on enhancing ease of use since so on the other hand um offers um Gran low level IPI and high level stream abstractions while since s flexibility is Advantage uh it's uh it requires uh developers to manage more some small details um simplification in can lead to faster development times

but may limit fine control conversely as flexibility C and detailed operations allows for more customized solution at the cost of simp of course and um yeah let's explore the Practical impacts of these API choices through example of n communication this example illustrates implementing a basic chart server simple uh using both Trio and a syn with tri the obstruction layer allows developers to focus on the coral L

without deling into details of a synchronous management and this uh in quick development and uh reduces its potential for bu related to task handling a s model offers flexibility to control every aspect uh and this can be beneficial for optimizing performance and customizing behavior for specific so next problem is integration with existing code and uh integrating a synchronous framework into existing systems can be challenging of course

and to requirement for significant adaptation uh stems from its structured concurrency model to integrate too effectively Legacy synchronous code might be restructured and we need to utilize new series for task management so the shift is uh to structur concurrency can dramatically change sometimes how tasks are created man and synchronized while this can enhance safy and maintainability it requires considerable effort to understanding and restructurizing your projects on

the other hand s integration is often smoother due to its flexible event Loop and task model so s allows for incremental adoption where only parts of the system need to be as synchronous and uh this flexibility makes a in appealing for projects that need gradual integration without complete overhauls um but actually you can also combine these two libraries together and we have Trio syo it's a p

library that Bridges the structured concurrency of Trio with a s flexibility this library is designed to allow developers to use St Str of Trio and a syn within the same application and using Trio s project can benefit from through structured approach and Broad capability over insute libraries this integration facilitat a smoo transition for projects requiring features from both Frameworks the library provides a unique opportunity to experiment

with both Frameworks without committing to one or the other so let's look at it uh this Library offers valuable tools like Trio as AO and AO as Trio you can simply convert functions function calls between Trio and a syn and uh this tools enables functions written for one framework to operate within the context of the other framework and uh so this flexibility alls developers to maintain existing

quod in one style while experimenting with other and by using Tri syn application can leverage the benefits of both libraries without extensive refactoring and this capability is especially useful in mixed environments where certain tasks are better suited to one model over the other and you can Choice um next like let's look at U updates uh for 2023 and 24 of Tri library and all this recent developments

have focused on improving scalability on ER handling and integration with systems we'll explore key new features that have been developed based on community feedback and some evolving programming practices um the first one it uh threat completion weight yeah threat completion weight is a crucial feature designed to um shutdowns are clear in multi environments yeah and this feature has us uh true's ability to handle as synchronous programming

alongside traditional multi tring uh it ensures that all threads have completed their task before the main uh are concluded yeah and this addition is particularly useful in environments where concurrent operations depend on task that must be careful synchronized so jck completion weight provides a more reliable method for shutting down systems without losing data by ensuring that no threats are left running it prevents resource leakage and ensure

all tasks are cleanly wrapped up before shut down and um this feature is significant step toward making to a safer framework for complex um wait for transition is crucial in systems where clearly shut DS are necessary to prevent issues and uh it introduces the function V Al TRS completed it designed exactly for this purpose it blocks that uh task execution until all threats spawn during three operations

have completed it's ensuring a correct shut down process and uh this function is especially Ed in server environments where multiple clients connections can carefully managed and using this function in multier server scenario helps manage client connection more reliable after the server M Lop exits uh this method ensures that all client connections are properly closed and uh this prevents the server from accidentially shutting down while background task

are still in progress which is could lead to resarch leaks and some data Corruptions so we can see it's importance in maintaining the Integrity of the network based applications um two recent adoptions of the pp Library enhances its file system interaction capabilities um integrating PFF lips aligns to you with Python's standard tools for path manipulation it's making file operation more intuitive and this feature enables developers to

perform file and directory operations without within some a synchronous environment seamlessly almost and uh by leveraging P liip Trio offers more ponic approach to asynchronous file management so this integration simplifies the transition for developers new to as synchronous programming by using wellknown patterns we'll explore how this changes make file operations easier to implement efficient um Tri path object is now fully integrated with path lip pure path

object it's uh offering a unified interface for file PA operations and uh this change enables natural integration uh with other AP types enhanc usability methods like it deer and R text have to support trios synchronous operations and it's making them non blocking yeah this example on the left demonstrates how we can asynchronously read directory context using tros enhanced path interface and this evation allows developers to handle

F system tasks using familiar and intuitive pu patterns consider the scenario on the right which involves managing numerous files across directories and uh using trios path operations like copying and moving files handled almost as synchronously and uh enhancing uh efficiency and yeah uh Fair allocation the method Globe is used to identify specific files and uh they will be copied to back application synchronously this integration not only

makes file operations not blocking but also more reliable and easier to manage in concurrent environments um to version 0.24 introduces a new testing helpers designed to streamline the testing of exception handling these helpers are particularly useful for testing exception groups we've spoken earlier and the integration of these tools reflects TR commitment to making robust and effective testing more accessible so let's look at it the new testing

helpers raises group and mat both are crucial for Effective managing trios exception groups during testing uh raises group allows developers to specify the expect multiple exceptions within a single test scenario and mat is used to uh assert specific details about uh exception within an exception group such as types or messages and this combination provides a powerful tool set for precisely verifying the behavior of code under the

some specific error conditions this example here uh demonstrates how we can we can use Network application to handle test multiple types simultaneously yeah and uh by using raises group and maters developers can create more detailed and robust test for system that handle complex error scenarios uh these tools ensure that uh all expected exceptions are correctly handled and their properties match their expected conditions redefin it um there

latest updates enhances its type checking capabilities uh to prevent common box in synchronous code yes and then new has Type checken feure utilizes Type R tole it's improving flexibility and safety of type annotations in many two functions uh this update is designed to help developers cat type related errors early significantly reducing runtime issues and uh enhan type Chen is uh especially available in Dynamic environments where type

Mis mates can lead to complex boxs um so by implementing more strip type checks still helps ensure that functions are used uh with correct types in overall C safety let's examine the setion types uh the incorporation of typ orle into trios type system allows for more s sophistic ated type checks across as synchronous functions this feure enables Trio to perform checks on variable argument lists ensuring that

all past arguments mat expected types and by enhancing type safety makes it easier developer to catch and fix all the type related errors during the development process before the r time uh consider the implementation of a type save synchronized database query Handler where each parameter types is validated against accepted type the St uh safety ensures that the function handling of query and its parameters are correctly typed

and uh using these type checks developers can enforce stricter type constraints which can lead to more reliable and more error resistant code um the next one is n exit Behavior Uh 2 0.3.3 sorry 23 introduces uh improvements in nursary exit behavior and this update enhances reliability of a synchronous task within nurseries and when exiting Nursery Trio ensures that the parent task waits for all field task for

complete completion correctly and um this Behavior guarantees that no task are left handing uh which could lead to some data loss or maybe corra State and um this enhancement is crucial for application requiring robust task synchronization and precise control over task execu task control of T competitions yeah um so let's examine the impact of these improvements um the update to nurser exit Behavior ensur that parent tasks

reliably wait for all child tasks before exiting the synchronization enhancement prevents the early termination of parent tasks which was a common issue in previous versions uh previously if child task were still running and early exit could disrupt the workflow and lead to mismatches so the improved Behavior includes handing of canellation attempts more ly and it's avoiding the injection of cancelled exceptions consider scenario where an application manages

multiple concurrent database queries uh with the updated Behavior All queries must complete before uh the system processed and data processing and it's ensuring data consistency and integrity so this ensures smoother operations and more reliable data handling particularly in ation where timing and order of operations have some importantance for us uh the next feature uh trade and task integration TR 0.23 also brings a significant improvements to how

threats and tasks integrate interact and uh it's enhancing concurrency management this ensure that better context and constellation propagation between threads and through Main Event Loop yeah uh improvements focus on making task execution within threads more predictable and more reliable and let's explore specific examples um this approach to trade integration focuses on making task context and state during transitions let's look at uh run sync function this function

is crucial it's enable and synchronous functions uh to run in separate trads without blocking the main Loop uh this capability is especially useful in applications like web scrapers that need to perform multiple synchronous operations concurrently using run scn developers can handle synchronous operations in a non-blocking manner such as making HTTP requests for simultaneously um this method allows the trio events Loop to continue managing the other synchronous

task effectively it's a great optimizing overall application performance and U consider um a synchronous web scraper designed to Fage data from multiple URLs simultaneously by executing HTTP request in separate threats the main synchronous flow remains unlocked and more responsive for our purpose this not only enhances performance but also improves the scalability application and as you can see um the next one um the same version to 0.23

introduces uh refined capabilities in guest run initialization it's enhancing how we handle uh external Integrations with some other libraries um this update allows Developers to immediately utilize Tri specific operations uh once uh initialization is complete so guest rization is particularly useful for applications uh that require integration with some non libraries uh this feature ensures that trios asynchronous environments is ready to handle tasks as soon as setup

is complete so by providing smoother transitions between synchronous and the synchronous quote it opens up more possibilities for diverse application architectures uh we'll explore how this implementing initialization can facilitate the use of Trio in systems where external libraries play a crucial role and let's see it so Duos enhanced gastron anization now includes more setup organization forting control to the main program it does a bit more uh

job for us uh this change allows for immediate use of trios specific apis right after initialization consider a scenario where a synchronous database operation need to and make it seamlessly and with some Library uh with the refined gestation J can manage asynchronous tasks alongside operations initiated by external libraries this functionality is crucial for application that depends on external systems for data operations but want to maintain a

synchronous effectively yeah this rization not only streamlines interactions with external libraries but also enhances the robust of this integration by ensuring that TR environments is fully operational immediately after set up uh developers can avoid common pitfalls associated with the synchronous Integrations yeah Tri 22 introduces the capability to name trads created with Runing function uh we can specify the name and send it to parameter to run syn

so we've learned a lot about Tri through this presentation as we move to summarize let's recall the key takeaways from a discussion to makes handling multiple tasks uh simultaneously easier and safe thanks to its structured concurrency and uh we started by exploring how nurseries provide safer space for task execution and examine previous robust eror handling mechanisms uh um discuss checkpoints and timeouts semors memory channels and also

compared Tri is c um these are updates from p flip integration to Advanced up cheing equip developers with powerful tool to tackle mod synchronous programing challenges um yeah so finally just want to say thank you for all for your attention today and thanks to D days for hosting this session hope uh you found the discussion informative and maybe um provided use avilable insights to use in you

for your projects so yeah if you have any questions feel feel free to ask now or contact me later and uh look forward to seeing you future events I'll collaborating on some open source projects maybe thank you awesome thanks a lot so we are unfortunately a little bit over time therefore Maybe Just One Last Question um are there any reasons for not using them Trio for instance

over ASN IO mhm true is not designed to be much more uh boost of performance in your application is designed for simplify your development and uh it can boost your the speed of your development process but it's not designed to make some any performance if you need to extremely fast make it in C or C++ yeah all right wonderful then thanks again for the nice session and

uh everyone else enjoy the conference next talks up in couple of minutes bye thank you bye

From event

DevDays Europe 2025

20 May 2025 – 23 May 2025

All event videos
Back to Watch