Home >Backend Development >Python Tutorial >When should I use Python 3.5\'s `await` and when should I avoid it?

When should I use Python 3.5\'s `await` and when should I avoid it?

Susan Sarandon
Susan SarandonOriginal
2024-11-21 12:45:16717browse

When should I use Python 3.5's `await` and when should I avoid it?

When to Embrace and When to Avoid Python 3.5's await

In Python 3.5, the introduction of asyncio and await brings asynchronous programming to the forefront. This paradigm shift allows for greater concurrency and faster execution, yet its boundaries can be confusing. This article delves into the subtleties of when to employ and when to steer clear of await.

Embracing await for I/O Operations

The primary rationale for embracing await lies in its ability to harness the power of asynchronous code. Consider I/O operations like network requests or file reads. Synchronous execution of these operations typically blocks the entire program, forcing it to wait for the completion of each task. In contrast, asynchronous code allows these operations to be executed concurrently, freeing up the program to handle other tasks.

# Synchronous Way
download(url1)  # takes 5 sec.
download(url2)  # takes 5 sec.
# Total time: 10 sec.

# Asynchronous Way
await asyncio.gather(
    async_download(url1),  # takes 5 sec. 
    async_download(url2)   # takes 5 sec.
)
# Total time: 5 sec. (plus asyncio overhead)

In the example above, the asynchronous approach completes in half the time, effectively minimizing wasted time spent waiting for I/O operations.

Refraining from Misuse of await for Synchronous Code

While await excels in handling I/O operations, it should not be applied indiscriminately to synchronous code. Synchronous code, such as CPU-bound tasks or simple data manipulation, does not benefit from the asynchronous paradigm and can actually introduce unnecessary overhead.

# Synchronous Code That Should Remain Synchronous
def parse(html):
    # This function does not involve any I/O operations.
    links = extract_links_from_html(html)
    return links

Casting synchronous code as asynchronous serves no purpose and can lead to a degradation in performance.

Special Considerations for Long-Running Synchronous Operations

A crucial aspect to consider when using asyncio is the impact of long-running synchronous operations on the asynchronous ecosystem. Synchronous operations that take a prolonged period to execute (e.g., over 50 ms) can potentially freeze all running asynchronous operations.

async def extract_links(url):
    data = await download(url)
    links = parse(data)
    # If search_in_very_big_file() takes a long time,
    # all ongoing async functions (elsewhere in code) will be suspended.
    links_found = search_in_very_big_file(links)

To prevent this freezing, consider executing such long-running operations in a separate process or using thread pools for I/O-bound operations within the asynchronous context.

In conclusion, await in Python 3.5 should be employed judiciously when dealing with I/O operations to harness the benefits of concurrency. However, it should be avoided for synchronous code and long-running operations within the asynchronous ecosystem to prevent potential performance pitfalls and bottlenecks. Careful consideration of these principles will ensure effective utilization of Python 3.5's asynchronous capabilities.

The above is the detailed content of When should I use Python 3.5's `await` and when should I avoid it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn