-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
Support multiple serverless platforms #86
Comments
So initially this is something I was considering here and Mangum used to support Azure specifically, but I ended up splitting that support out into a separate package https://github.com/erm/bonnette and have done something similar with Google Cloud Functions in https://github.com/erm/grevillea. The adapter logic is essentially the same in these projects, but I don't really maintain them. It has crossed my mind to re-add support for multiple providers (or find some way of sharing the adapter logic across the separate projects), but I'm not really sure. I'll leave this issue open, if there is enough interest I'll take a serious look into doing so. |
That's nice to hear, and thanks for the pointers. The reason I posted this issue is because I think it would be great to be able to use the awesome FastAPI package across AWS Lambda, Azure Functions, and Google Cloud Functions. If it helps, here's one other example of people with the same interest: fastapi/fastapi#812 |
I was looking to use FastAPI with Google Cloud Funtions and landed over here. Agree with @lsorber . |
@Nagalenoj Okay, I'll think about how to approach this, but I probably won't be able to start on it for awhile, not sure when. Most likely I will support multiple providers in this project. |
So I started doing some investigation into how I might accomplish this. I'm thinking adapter classes will look like this: @dataclass
class Adapter:
options: dict
def __call__(self, *args) -> Any:
raise NotImplementedError
def on_request(self) -> None:
raise NotImplementedError
def on_response(self) -> None:
raise NotImplementedError
def on_response_error(self) -> None:
raise NotImplementedError The @dataclass
class Mangum:
"""
Creates an adapter instance.
* **app** - An asynchronous callable that conforms to version 3.0 of the ASGI
specification. This will usually be an ASGI framework application instance.
* **lifespan** - A string to configure lifespan support. Choices are `auto`, `on`,
and `off`. Default is `auto`.
* **provider** - A string value identifier for the platform. Choices are `aws`,
`azure`, and `google`. Default is `aws`.
* **options** - A dictionary mapping of provider-specific configuration.
"""
app: ASGIApp
lifespan: str = "auto"
provider: str = "aws"
options: dict = field(default_factory=dict)
def __post_init__(self) -> None:
if self.lifespan not in ("auto", "on", "off"): # pragma: no cover
raise ConfigurationError(
"Invalid argument supplied for `lifespan`. Choices are: auto|on|off"
)
if self.provider == "aws":
from mangum.adapters.aws import AWSAdapter
self.adapter = AWSAdapter(self.options)
elif self.provider == "azure":
from mangum.adapters.azure import AzureAdapter
self.adapter = AzureAdapter(self.options)
elif self.provider == "google":
from mangum.adapters.google import GoogleAdapter
self.adapter = GoogleAdapter(self.options)
def __call__(self, *args) -> dict:
with ExitStack() as stack:
if self.lifespan != "off":
lifespan_cycle: typing.ContextManager = LifespanCycle(
self.app, self.lifespan
)
stack.enter_context(lifespan_cycle)
http_cycle = self.adapter(*args)
response = http_cycle(self.app)
return response My intention is to implement this with support for Azure and GCF to start since I already have some work to base them on, but it should be able to support any similar platforms. Ideally I can find a way to determine the platform at runtime without specifying the |
After doing some further investigation I ran across Azure/azure-functions-python-worker#165. The The maintenance burden of keeping up with these external developments seems quite high, especially since I only really use AWS Lambda myself, so it seems to make more sense to not include this here. There are still potentially ways to share the generic behaviour for other use-cases/packages - I'll have to think about what this means exactly, will keep this issue open and update it with my thoughts later. |
I created an issue here to explore the Azure use-case. |
Makes sense @jordaneremieff. Thank you for taking the time to explore what a solution could look like, and for helping to kick-start a proper integration. Unrelated: you've got some creative project names, how do you come up with those if I may ask? |
@lsorber No worries. I think most of what is in here can be re-purposed without too much effort. I'll base anything further about this on how the Azure functions library might approach ASGI, will just let this ticket hang around in the meantime. I might just end up copying over anything relevant from Mangum to Grevillea specifically when I next look into GCF. Also, Mangum & Bonnette are named after my two favourite musicians, Jeff Mangum of Neutral Milk Hotel and Sean Bonnette of AJJ. Grevillea (spider flower) is the genus of my favourite plant, Grevillea Buxifolia. :) |
This comment [1] explains how to use Mangum with the serverless framework. Since serverless supports multiple providers (e.g., Azure), would it be possible that Mangum enables ASGI support for other providers too?
[1] https://dev.to/anilgrexit/comment/gkfo
The text was updated successfully, but these errors were encountered: