-
Notifications
You must be signed in to change notification settings - Fork 121
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
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9ea1d5c
feat: add ASGI server support for async functions
taeold 80385e8
fix: apply black and isort formatting to source files
taeold 069eb3e
test: add comprehensive tests for ASGI server support
taeold 734add5
fix: skip async test files on Python 3.7
taeold bbd1f12
fix: exclude async code from Python 3.7 coverage
taeold 6355e9b
fix: Install uvicorn on windows.
taeold bd17bcf
fix: unable to use reload in starlette.
taeold 2311428
fix: add missing endpoint parameter to Route constructors in ASGI
taeold 8b30eab
feat: add async conformance tests with ASGI gateway
taeold 9f3facb
fix: set UvicornWorker class before parent init and update tests
taeold 1b54e88
fix: update asgi tests to remove reload option
taeold a3405c8
feat: add async-specific conformance tests for ASGI mode
taeold ca13753
fix: apply black formatting to async files
taeold 7134c91
fix: disable validateMapping for CloudEvent tests in ASGI mode
taeold 9dc35e8
fix: avoid mutating options dict in Gunicorn applications
taeold 11e63db
fix: add pragma comments for Python 3.7 coverage and fix options hand…
taeold 1b02275
fix: revert to separate GunicornApplication and UvicornApplication cl…
taeold 89fd645
refactor: restore GunicornApplication to match main branch
taeold 638a88e
chore: Untrack uv.lock
taeold 68d8076
chore: rename confirmance test (asgi) github workflow
taeold aa39dcc
chore: cleanup .gitignore.
taeold 548ef3c
chore: clean up unncessary comments.
taeold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
name: Python Conformance CI (asgi) | ||
on: | ||
push: | ||
branches: | ||
- 'main' | ||
pull_request: | ||
|
||
# Declare default permissions as read only. | ||
permissions: read-all | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
python: ['3.8', '3.9', '3.10', '3.11', '3.12'] | ||
platform: [ubuntu-latest] | ||
runs-on: ${{ matrix.platform }} | ||
steps: | ||
- name: Harden Runner | ||
uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0 | ||
with: | ||
disable-sudo: true | ||
egress-policy: block | ||
allowed-endpoints: > | ||
api.github.com:443 | ||
files.pythonhosted.org:443 | ||
github.com:443 | ||
objects.githubusercontent.com:443 | ||
proxy.golang.org:443 | ||
pypi.org:443 | ||
storage.googleapis.com:443 | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
|
||
- name: Setup Python | ||
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 | ||
with: | ||
python-version: ${{ matrix.python }} | ||
|
||
- name: Install the framework with async extras | ||
run: python -m pip install -e .[async] | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 | ||
with: | ||
go-version: '1.24' | ||
|
||
- name: Run HTTP conformance tests | ||
uses: GoogleCloudPlatform/functions-framework-conformance/action@72a4f36b10f1c6435ab1a86a9ea24bda464cc262 # v1.8.6 | ||
with: | ||
functionType: 'http' | ||
useBuildpacks: false | ||
validateMapping: false | ||
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_http --signature-type http --gateway asgi'" | ||
|
||
- name: Run CloudEvents conformance tests | ||
uses: GoogleCloudPlatform/functions-framework-conformance/action@72a4f36b10f1c6435ab1a86a9ea24bda464cc262 # v1.8.6 | ||
with: | ||
functionType: 'cloudevent' | ||
useBuildpacks: false | ||
validateMapping: false | ||
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_cloud_event --signature-type cloudevent --gateway asgi'" | ||
|
||
- name: Run HTTP conformance tests declarative | ||
uses: GoogleCloudPlatform/functions-framework-conformance/action@72a4f36b10f1c6435ab1a86a9ea24bda464cc262 # v1.8.6 | ||
with: | ||
functionType: 'http' | ||
useBuildpacks: false | ||
validateMapping: false | ||
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_http_declarative --gateway asgi'" | ||
|
||
- name: Run CloudEvents conformance tests declarative | ||
uses: GoogleCloudPlatform/functions-framework-conformance/action@72a4f36b10f1c6435ab1a86a9ea24bda464cc262 # v1.8.6 | ||
with: | ||
functionType: 'cloudevent' | ||
useBuildpacks: false | ||
validateMapping: false | ||
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_cloud_event_declarative --gateway asgi'" | ||
|
||
- name: Run HTTP concurrency tests declarative | ||
uses: GoogleCloudPlatform/functions-framework-conformance/action@72a4f36b10f1c6435ab1a86a9ea24bda464cc262 # v1.8.6 | ||
with: | ||
functionType: 'http' | ||
useBuildpacks: false | ||
validateConcurrency: true | ||
cmd: "'functions-framework --source tests/conformance/async_main.py --target write_http_declarative_concurrent --gateway asgi'" | ||
|
||
# Note: Event (legacy) and Typed tests are not supported in ASGI mode | ||
# Note: validateMapping is set to false for CloudEvent tests because ASGI mode | ||
# does not support automatic conversion from legacy events to CloudEvents |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ dist/ | |
function_output.json | ||
serverlog_stderr.txt | ||
serverlog_stdout.txt | ||
venv/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import uvicorn | ||
|
||
|
||
class StarletteApplication: | ||
"""A Starlette application that uses Uvicorn for direct serving (development mode).""" | ||
|
||
def __init__(self, app, host, port, debug, **options): | ||
"""Initialize the Starlette application. | ||
|
||
Args: | ||
app: The ASGI application to serve | ||
host: The host to bind to | ||
port: The port to bind to | ||
debug: Whether to run in debug mode | ||
**options: Additional options to pass to Uvicorn | ||
""" | ||
self.app = app | ||
self.host = host | ||
self.port = port | ||
self.debug = debug | ||
|
||
self.options = { | ||
"log_level": "debug" if debug else "error", | ||
} | ||
self.options.update(options) | ||
|
||
def run(self): | ||
"""Run the Uvicorn server directly.""" | ||
uvicorn.run(self.app, host=self.host, port=int(self.port), **self.options) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import asyncio | ||
import json | ||
|
||
from cloudevents.http import to_json | ||
|
||
import functions_framework.aio | ||
|
||
filename = "function_output.json" | ||
|
||
|
||
class RawJson: | ||
data: dict | ||
|
||
def __init__(self, data): | ||
self.data = data | ||
|
||
@staticmethod | ||
def from_dict(obj: dict) -> "RawJson": | ||
return RawJson(obj) | ||
|
||
def to_dict(self) -> dict: | ||
return self.data | ||
|
||
|
||
def _write_output(content): | ||
with open(filename, "w") as f: | ||
f.write(content) | ||
|
||
|
||
async def write_http(request): | ||
json_data = await request.json() | ||
_write_output(json.dumps(json_data)) | ||
return "OK", 200 | ||
|
||
|
||
async def write_cloud_event(cloud_event): | ||
_write_output(to_json(cloud_event).decode()) | ||
|
||
|
||
@functions_framework.aio.http | ||
async def write_http_declarative(request): | ||
json_data = await request.json() | ||
_write_output(json.dumps(json_data)) | ||
return "OK", 200 | ||
|
||
|
||
@functions_framework.aio.cloud_event | ||
async def write_cloud_event_declarative(cloud_event): | ||
_write_output(to_json(cloud_event).decode()) | ||
|
||
|
||
@functions_framework.aio.http | ||
async def write_http_declarative_concurrent(request): | ||
await asyncio.sleep(1) | ||
return "OK", 200 | ||
|
||
|
||
# Note: Typed events are not supported in ASGI mode yet | ||
# Legacy event functions are also not supported in ASGI mode |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remove whitespace?