Skip to content

Commit

Permalink
Avoid UserWarning when creating ClientSession outside a coroutine
Browse files Browse the repository at this point in the history
If an aiohttp.ClientSession is created outside a coroutine without a running
event loop, a warning is shown:
/Users/walkman/Nextcloud/Projects/simple-podcast-dl/podcast_dl/cli.py:210: UserWarning: Creating a client session outside of coroutine is a very dangerous idea
  http = aiohttp.ClientSession()
Creating a client session outside of coroutine
client_session: <aiohttp.client.ClientSession object at 0x102cb5ef0>

We can avoid it by explicitly passing an event loop. For details, see:
aio-libs/aiohttp#1468
  • Loading branch information
kissgyorgy committed Oct 26, 2018
1 parent 3ee1308 commit 4c8a858
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions podcast_dl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ def main(

vprint = click.secho if verbose else noprint

http = aiohttp.ClientSession()
atexit.register(http.close)

loop = asyncio.get_event_loop()
atexit.register(loop.close)

http = aiohttp.ClientSession(loop=loop)
atexit.register(http.close)

ensure_download_dir(download_dir)
rss_root = loop.run_until_complete(download_rss(http, podcast.rss))
all_rss_items = get_all_rss_items(rss_root, podcast.rss_parser)
Expand Down

0 comments on commit 4c8a858

Please sign in to comment.