feat: Add support for async functions #364
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces foundational support for defining and running Google Cloud Functions using Python's native async/await syntax within the Functions Framework. This allows developers to leverage the asynchronous ecosystem for I/O-bound operations directly in their function code.
For example, developers can now write:
The framework handles bootstrapping an ASGI server using Starlette to host and execute these asynchronous function targets, analogous to how Flask is used for existing WSGI functions.
Key Changes:
New
functions_framework.aio
Submodule: Introduced a dedicated submodule to house asynchronous-specific components, keeping implementation separated from the existing WSGI implementation.create_asgi_app: async version of
create_app
to bootstrap an asynchronous function target and wraps it into a Starlette ASGI application, handling request parsing and response formatting.Optional Dependency ([async] extra): Starlette and its dependencies (uvicorn likely needed later) are introduced as optional packages for functions_framework. Users must explicitly install the framework with the async extra to enable this functionality to avoid adding unnecessary dependencies for users who existing "synch"-only use cases.
The structure and internal handling within
create_asgi_app
attempt to mirror the existing WSGI implementation where feasible, aiming for consistency in request processing logic (e.g., header handling, response serialization).Explicitly Out of Scope (for this PR):
This PR is the first step towards full async support. Several key pieces are intentionally omitted and will be addressed in subsequent PRs:
@errorhandler
Support: Implementing error handling registration and execution for async functions, similar to the existing Flask-based error handlers.Partially addresses #192, #194