non-blocking operation

In Python, a non-blocking operation is an operation that allows your program to continue executing other tasks without waiting for the operation to complete. This is particularly useful when dealing with I/O-bound task such as reading from or writing to a file, making network requests, or interacting with databases.

By using non-blocking operations, you can enhance the efficiency and responsiveness of your programs, as they can perform other computations or handle multiple tasks concurrently while waiting for an input/output (I/O) operation to finish. These types of operations are a key feature in asynchronous programming.

Example

Here’s an example using Python’s asyncio module to demonstrate non-blocking operations with the async and await keywords:

Python
>>> import asyncio

>>> async def fetch_data():
...     print("Start fetching data...")
...     await asyncio.sleep(2)
...     print("Data fetched!")
...

>>> async def main():
...     task1 = asyncio.create_task(fetch_data())
...     task2 = asyncio.create_task(fetch_data())
...     await task1
...     await task2
...

>>> asyncio.run(main())
Start fetching data...
Start fetching data...
Data fetched!
Data fetched!

In this example, the fetch_data() function simulates a non-blocking I/O operation using asyncio.sleep(), which doesn’t block the event loop. In main(), you start two tasks. When task1 starts, the program continues executing without waiting for it to complete.

Tutorial

Async IO in Python: A Complete Walkthrough

This tutorial will give you a firm grasp of Python’s approach to async IO, which is a concurrent programming design that has received dedicated support in Python, evolving rapidly from Python 3.4 through 3.7 (and probably beyond).

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated May 6, 2025
OSZAR »