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

Aiohttp server Background task loop deprecated #3526

Closed
gcetusic opened this issue Jan 11, 2019 · 5 comments · Fixed by #3529
Closed

Aiohttp server Background task loop deprecated #3526

gcetusic opened this issue Jan 11, 2019 · 5 comments · Fixed by #3529
Labels

Comments

@gcetusic
Copy link
Contributor

gcetusic commented Jan 11, 2019

Long story short

In the docs there is an explanation on how to create a Background task (in this case, a Redis listener)

https://docs.aiohttp.org/en/stable/web_advanced.html

I want to create a background task to listen for Postgres notifications.

Expected behaviour

In the docs, the example is app.loop.create_task(listen_to_redis(app)) and this should be the official working example or a different one if this one is deprecated.

Actual behaviour

When running the code, a warning occurs:

gateway.py:39: DeprecationWarning: loop property is deprecated app['listener'] = app.loop.create_task(listen(app)) Traceback (most recent call last): File "gateway.py", line 117, in <module> web.run_app(app) File "/home/goc/.virtualenvs/doorway/lib/python3.7/site-packages/aiohttp/web.py", line 210, in run_app reuse_port=reuse_port)) File "/usr/local/lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete return future.result() concurrent.futures._base.CancelledError

Steps to reproduce

https://github.com/gcetusic/doorway/blob/master/gateway.py#L39

Your environment

Aiohttp server instance (version 3.5.3), Ubuntu 18.04

@asvetlov
Copy link
Member

Just drop loop parameter, all other is fine.
Would you prepare a Pull Request?

@gcetusic
Copy link
Contributor Author

gcetusic commented Jan 11, 2019

What would be the proper way to create the background task? Something like this?

loop = get_running_loop()
app['listener'] = loop.create_task(listen(app))

@asvetlov
Copy link
Member

Yes.
Python 3.7 has a handy helper asyncio.create_task: https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task

@rafa-acioly
Copy link

what should i use instead of .loop?

this is my case:

app = web.Application(loop=loop, middlewares=get_middlewares())
app.on_startup.append(load_plugins)
# ...
async def load_plugins(app):
    app.mongo = Mongo.create()
    app.mongo.initialize(app.loop)
    app.redis = await get_cache(loop=app.loop)

In this case is better to wait for an update of motor or there is another solution?

@gcetusic
Copy link
Contributor Author

@rafa-acioly I believe the idea is to still use the loop if a third party library requires it but it's instead of referencing app.loop, just use what you get from get_running_loop().
Why using app.loop is deprecated I don't know but this should have the same effect as the loops should be the same.

@lock lock bot added the outdated label Jan 30, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jan 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants