Production Deployments¶
The Microdot class creates its own simple web server. This is enough for an
application deployed with MicroPython, but when using CPython it may be useful
to use a separate, battle-tested web server. To address this need, Microdot
provides extensions that implement the ASGI and WSGI protocols.
Using an ASGI Web Server¶
Compatibility |
CPython only
|
Required Microdot source files |
|
Required external dependencies |
An ASGI web server, such as Uvicorn.
|
Examples |
The asgi module provides an extended Microdot class that
implements the ASGI protocol and can be used with a compliant ASGI server such
as Uvicorn.
To use an ASGI web server, the application must import the
Microdot class from the asgi module:
from microdot.asgi import Microdot
app = Microdot()
@app.route('/')
async def index(req):
return 'Hello, World!'
The app application instance created from this class can be used as the
ASGI callable with any complaint ASGI web server. If the above example
application was stored in a file called test.py, then the following command
runs the web application using the Uvicorn web server:
uvicorn test:app
When using the ASGI support, the scope dictionary provided by the web
server is available to request handlers as request.asgi_scope.
The application instance can be initialized with lifespan_startup and
lifespan_shutdown arguments, which are invoked when the web server sends
the ASGI lifespan signals with the ASGI scope as only argument:
async def startup(scope):
pass
async def shutdown(scope):
pass
app = Microdot(lifespan_startup=startup, lifespan_shutdown=shutdown)
Using a WSGI Web Server¶
Compatibility |
CPython only
|
Required Microdot source files |
|
Required external dependencies |
A WSGI web server, such as Gunicorn.
|
Examples |
The wsgi module provides an extended Microdot class that implements the
WSGI protocol and can be used with a compliant WSGI web server such as
Gunicorn or
uWSGI.
To use a WSGI web server, the application must import the
Microdot class from the wsgi module:
from microdot.wsgi import Microdot
app = Microdot()
@app.route('/')
def index(req):
return 'Hello, World!'
The app application instance created from this class can be used as a WSGI
callbable with any complaint WSGI web server. If the above application
was stored in a file called test.py, then the following command runs the
web application using the Gunicorn web server:
gunicorn test:app
When using the WSGI support, the environ dictionary provided by the web
server is available to request handlers as request.environ.
Note
In spite of WSGI being a synchronous protocol, the Microdot application
internally runs under an asyncio event loop. For that reason, the
recommendation to prefer async def handlers over def still applies
under WSGI. Consult the Concurrency section for a discussion of how
the two types of functions are handled by Microdot.