CherryPy traditionally handled requests synchronously, processing one request at a time per thread. However, with the introduction of Python's asyncio module and the asynchronous capabilities it provides, CherryPy has evolved to support asynchronous request handling.
Asynchronous Support in CherryPy :
* Async Tools and Decorators : CherryPy introduced asynchronous support through decorators like
@cherrypy.tools.asyncio()
, allowing developers to mark specific methods or endpoints within their CherryPy application as asynchronous.
import cherrypy
class MyAsyncApp:
@cherrypy.expose
@cherrypy.tools.asyncio()
async def async_endpoint(self):
# Asynchronous handling here
pass​
* Asyncio Integration : CherryPy leverages Python's
asyncio
framework to handle asynchronous I/O operations, enabling CherryPy applications to perform non-blocking I/O operations and concurrent handling of requests.
* Concurrency and Non-blocking Operations : By using asynchronous mechanisms, CherryPy can handle multiple requests concurrently without blocking the execution flow. This is particularly useful when dealing with I/O-bound operations like database queries or network requests.
* Event Loop Integration : CherryPy works within the asyncio event loop, allowing it to await asynchronous operations using await expressions within async methods.
* Improved Scalability : Asynchronous support in CherryPy enhances its scalability by enabling better utilization of system resources, allowing for more efficient handling of a larger number of concurrent requests.
* Performance Benefits : Applications that have a significant portion of I/O-bound operations can benefit from CherryPy's asynchronous support, as it reduces the overhead of waiting for I/O operations to complete before handling subsequent requests.
Considerations and Compatibility :
* Asynchronous support in CherryPy is compatible with Python 3.7 and later versions due to its reliance on the
asyncio
module, which was introduced in Python 3.7.
* Not all parts of CherryPy may support asynchronous handling out of the box. Some extensions or custom components might require adjustments to fully support asynchronous operations.