Async / Sync
Asynchronous programming is a powerful way to handle multiple tasks concurrently without blocking the execution of your program. Chainlit is async by default to allow agents to execute tasks in parallel and allow multiple users on a single app.
Python introduced the asyncio
library to make it easier to write asynchronous code using the async/await
syntax. This onboarding guide will help you understand the basics of asynchronous programming in Python and how to use it in your Chainlit project.
Understanding async/await
The async
and await
keywords are used to define and work with asynchronous code in Python. An async
function is a coroutine, which is a special type of function that can pause its execution and resume later, allowing other tasks to run in the meantime.
To define an async function, use the async def
syntax:
To call an async function, you need to use the await
keyword:
Working with Chainlit
Chainlit uses asynchronous programming to handle events and tasks efficiently. When creating a Chainlit agent, you’ll often need to define async functions to handle events and perform actions.
For example, to create an async function that responds to messages in Chainlit:
Long running synchronous tasks
In some cases, you need to run long running synchronous functions in your Chainlit project. To prevent blocking the event loop, you can utilize the make_async
function provided by the Chainlit library to transform a synchronous function into an asynchronous one:
By using this approach, you can maintain the non-blocking nature of your project while still incorporating synchronous functions when necessary.
Call an async function from a sync function
If you need to run an asynchronous function inside a sync function, you can use the run_sync
function provided by the Chainlit library:
By following this guide, you should now have a basic understanding of asynchronous programming in Python and how to use it in your Chainlit project. As you continue to work with Chainlit, you’ll find that async/await and the asyncio library provide a powerful and efficient way to handle multiple agents/tasks concurrently.
Was this page helpful?