Python Workers now support WSGI web frameworks like Django and Flask
Python web frameworks following the Web Server Gateway Interface (WSGI) ↗ or Asynchronous Server Gateway Interface (ASGI) ↗ specification can now be used in Python Workers.
Based on the web framework you are using, you can use either wsgi or asgi from the workers module.
For WSGI frameworks like Django or Flask:
from workers import wsgi
from django.core.wsgi import get_wsgi_application
app = get_wsgi_application()
Default = wsgi.entrypoint(app)The wsgi.entrypoint is equivalent to creating a WorkerEntrypoint class and using the wsgi.fetch method. If you want more control over the WorkerEntrypoint class, you can do so:
from workers import wsgi, WorkerEntrypoint
class Default(WorkerEntrypoint):
async def fetch(self, request):
return await wsgi.fetch(app, request, self.env)For ASGI frameworks like FastAPI or Starlette:
from workers import asgi
from fastapi import FastAPI
app = FastAPI()
Default = asgi.entrypoint(app)For more information about using individual web frameworks, refer to the packages documentation in Python Workers.