pgjones / quart-trio

Quart-Trio is an extension for Quart to support the Trio event loop.
MIT License
20 stars 9 forks source link

unable to start background_task #18

Open zoranbosnjak opened 1 month ago

zoranbosnjak commented 1 month ago

I am trying to run a loop concurrently with the web server, like this:

import trio
from quart_trio import QuartTrio

app = QuartTrio(__name__)

async def background_task():
    while True:
        print(trio.current_time())
        await trio.sleep(1.0)

@app.route('/')
async def hello():
    return 'hello'

app.add_background_task(background_task)
app.run()

From the documentation it looks like I need a background_task. But when running this example, I am getting this exception: AttributeError: 'QuartTrio' object has no attribute 'nursery'. Is there something wrong in the code or a bug? I could not find any solution in the docs. Appreciate any suggestion.

zoranbosnjak commented 1 month ago

Answering my own question..., it looks like I need to do this:

@app.before_serving
async def startup():
    app.add_background_task(background_task)

But there is another question. Suppose that a background_task is mandatory for the web application. Is there a way to force stop the web application automatically if the background task returns or crashes?