-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3.0: Single-callable style applications & lifecycle improvements (#80)
This switches the ASGI specification to use a single, async callable, as well as improving the error behaviour of the lifecycle scope type.
- Loading branch information
1 parent
e4015bf
commit 8ae25bd
Showing
7 changed files
with
250 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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,47 @@ | ||
import asyncio | ||
import inspect | ||
|
||
|
||
def is_double_callable(application): | ||
""" | ||
Tests to see if an application is a legacy-style (double-callable) application. | ||
""" | ||
# Look for a hint on the object first | ||
if getattr(application, "_asgi_single_callable", False): | ||
return False | ||
if getattr(application, "_asgi_double_callable", False): | ||
return True | ||
# Uninstanted classes are double-callable | ||
if inspect.isclass(application): | ||
return True | ||
# Instanted classes depend on their __call__ | ||
if hasattr(application, "__call__"): | ||
# We only check to see if its __call__ is a coroutine function - | ||
# if it's not, it still might be a coroutine function itself. | ||
if asyncio.iscoroutinefunction(application.__call__): | ||
return False | ||
# Non-classes we just check directly | ||
return not asyncio.iscoroutinefunction(application) | ||
|
||
|
||
def double_to_single_callable(application): | ||
""" | ||
Transforms a double-callable ASGI application into a single-callable one. | ||
""" | ||
|
||
async def new_application(scope, receive, send): | ||
instance = application(scope) | ||
return await instance(receive, send) | ||
|
||
return new_application | ||
|
||
|
||
def guarantee_single_callable(application): | ||
""" | ||
Takes either a single- or double-callable application and always returns it | ||
in single-callable style. Use this to add backwards compatibility for ASGI | ||
2.0 applications to your server/test harness/etc. | ||
""" | ||
if is_double_callable(application): | ||
application = double_to_single_callable(application) | ||
return application |
This file contains 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 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 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 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 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
Oops, something went wrong.