Skip to content
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

unable to start background_task #18

Open
zoranbosnjak opened this issue Jul 24, 2024 · 1 comment
Open

unable to start background_task #18

zoranbosnjak opened this issue Jul 24, 2024 · 1 comment

Comments

@zoranbosnjak
Copy link

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
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant