In this tutorial, you’ll learn how to work with asynchronous functions in Python, which helps to manage simultaneous tasks or activities with more efficiency, particularly when dealing with input-output bound situations like web scraping or API calls.
Using the asyncio library in Python can save time when executing multiple requests at the same time rather than using the synchronous approach.
Before starting, ensure that you are using Python 3.7 or higher, as this tutorial assumes the use of new asyncio features available in these versions.
Step 1: Install and import necessary libraries
To use async functions in Python, you need to have the asyncio library installed. You can install this library using pip:
1 |
pip install asyncio |
Next, import the required libraries in your Python script:
1 2 |
import asyncio import time |
Step 2: Create an asynchronous function with async/await
To create an asynchronous function, use the async def
keyword for defining the function. Inside the async function, use the await
keyword for asynchronous calls that should be executed concurrently. Here’s an example of a simple async function that simulates a delay using asyncio.sleep
:
1 2 3 4 |
async def async_function(): print("Starting async function...") await asyncio.sleep(1) print("Async function completed!") |
Step 3: Run the async function in an event loop
To run an async function, you need to use an event loop, which is responsible for scheduling and executing asynchronous tasks in a concurrent way. Here’s how to create a simple event loop that runs the async_function created above:
1 2 3 4 5 |
async def main(): await async_function() loop = asyncio.get_event_loop() loop.run_until_complete(main()) |
This will execute the async_function()
and wait for it to complete.
Step 4: Running multiple async functions concurrently
To run multiple async functions concurrently, use asyncio.gather()
:
1 2 3 4 5 6 |
async def main(): tasks = [async_function() for _ in range(5)] await asyncio.gather(*tasks) loop = asyncio.get_event_loop() loop.run_until_complete(main()) |
This example will run 5 instances of the async_function()
concurrently, and the main()
function will complete only after all 5 tasks are finished.
Full code example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import asyncio async def async_function(): print("Starting async function...") await asyncio.sleep(1) print("Async function completed!") async def main(): tasks = [async_function() for _ in range(5)] await asyncio.gather(*tasks) loop = asyncio.get_event_loop() loop.run_until_complete(main()) |
In this example, you will see that all 5 instances of the async function start at the same time, and then complete a second later.
Starting async function... Starting async function... Starting async function... Starting async function... Starting async function... Async function completed! Async function completed! Async function completed! Async function completed! Async function completed!
Conclusion
In this tutorial, you’ve learned how to create and run async functions in Python using the asyncio library. This method can improve the efficiency of your code by allowing multiple tasks to run concurrently, improving the overall speed of execution.