Skip to content

feat: add flag to run functions framework in asgi stack #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jun 12, 2025

Conversation

taeold
Copy link
Contributor

@taeold taeold commented Jun 12, 2025

This PR introduces a new flag to enable ASGI support to the Functions Framework for Python allowing users to run asynchronous functions.

Key changes:

  • A new --gateway flag. The available options are wsgi (default) and asgi.
  • For ASGI, the framework uses Starlette in debug mode and uvicorn worker for Gunicorn in production.
  • The async optional dependencies have been updated to include uvicorn and uvicorn-worker.
  • New conformance tests for ASGI have been added to ensure compatibility.

Note: This PR does not include support for execution ID and Jinja2 templates for ASGI applications. This will be addressed in a subsequent PR.

taeold and others added 22 commits June 11, 2025 17:00
- Add uvicorn and uvicorn-worker to async optional dependencies
- Refactor gunicorn.py with BaseGunicornApplication for shared config
- Add UvicornApplication class for ASGI apps
- Add StarletteApplication in asgi.py for development mode
- Update HTTPServer to auto-detect Flask (WSGI) vs other (ASGI) apps
- Add --gateway CLI flag to choose between wsgi and asgi
- Update test_http.py to use Flask instance in tests
- Add tests for HTTPServer ASGI/WSGI auto-detection
- Add tests for StarletteApplication and UvicornApplication
- Add tests for CLI --gateway flag functionality
- Add integration tests for async functions with ASGI
- Ensure 100% code coverage for new ASGI features
- Apply black and isort formatting to test files
ASGI mode does not support automatic conversion from legacy events to CloudEvents, so validateMapping must be false for CloudEvent conformance tests.
Create a copy of the options dict before modifying it to prevent side effects when the same options dict is reused elsewhere. This could cause issues with timeout tests.
…ling

- Add pragma: no cover comments for ASGI-specific code paths that won't execute in Python 3.7
- Fix options dict handling to use consistent variable names to avoid confusion
…asses

Remove the BaseGunicornApplication abstraction as it was causing issues with the timeout mechanism. Each class now independently extends gunicorn.app.base.BaseApplication, which is cleaner and avoids the problems we were seeing with shared state and options handling.
Remove unnecessary changes to GunicornApplication class, keeping only
the UvicornApplication addition for ASGI support.
@taeold taeold marked this pull request as ready for review June 12, 2025 20:01
Copy link
Member

@maemayve maemayve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for this contribution. I appreciate the parameterized tests.

@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from flask import Flask

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove whitespace?

@maemayve maemayve merged commit 268acf1 into GoogleCloudPlatform:main Jun 12, 2025
68 of 86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants