Closed superstarrr closed 7 years ago
This is strange, I don't see how this can happen. Could you please share the code so that I can debug this?
Finally I figure it out, it is caused by the incorrect position of installing code.
The installing code is put under one imported module which creates an instance of aiohttp.ClientSession()
, for example,
# main.py
import asyncio
from foo import Foo
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
async def fetch(url):
foo = Foo()
async with foo.session.get(url) as resp:
# RuntimeError will be raised here
pass
loop = asyncio.get_event_loop()
loop.run_until_complete(fetch('http://example.com'))
# foo.py
import aiohttp
class Foo():
session = aiohttp.ClientSession() # to share session between instances of Foo
Hrrr.
You are creating a global var here (class variable actually). It is anti-pattern with very bad smell.
I have to admit that it is a bad idea. And will remove these lazy code. Thanks!
Should this issue be closed now?
OK, closing this.
Hi guys,
My program runs normally when using default asyncio policy. But if installing the uvloop event loop policy,
the following exception will be raised:
Thanks!