@app.before_serving
async def startup():
loop = asyncio.get_event_loop()
app.smtp_server = loop.create_server(aiosmtpd.smtp.SMTP, port=1025)
loop.create_task(app.smtp_server)
@app.after_serving
async def shutdown():
app.smtp_server.close()
Do not follow this pattern, typically seen in examples, because this creates a
new loop separate from the Quart loop for ThirdParty,
loop = asyncio.get_event_loop()
third_party = ThirdParty(loop)
app.run() # A new loop is created by default
Controlling the event loop
It is the ASGI server running running Quart that owns the event loop
that Quart runs within, by default the server is Hypercorn. Both Quart
and Hypercorn allow the loop to be specified, the Quart shortcut in
development is to pass the loop to the app.run
method,
loop = asyncio.get_event_loop()
third_party = ThirdParty(loop)
app.run(loop=loop)
or to use the app.run_task
method,
loop = asyncio.get_event_loop()
third_party = ThirdParty(loop)
loop.run_until_complete(app.run_task())
the Hypercorn (production) solution is to utilise the Hypercorn API to do the
following,
from hypercorn.asyncio import serve
from hypercorn.config import Config
loop = asyncio.get_event_loop()
third_party = ThirdParty(loop)
loop.run_until_complete(serve(app, Config()))
# or even
await serve(app, config)