Home > Article > Backend Development > When should I use Python 3.5\'s `await` and when should I avoid it?
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.
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.
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.
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!