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

uvloop + aiohttp RuntimeError: Timeout context manager should be used inside a task #63

Closed
superstarrr opened this issue Dec 8, 2016 · 6 comments

Comments

@superstarrr
Copy link

superstarrr commented Dec 8, 2016

  • uvloop version: 0.6.7
  • python version: 3.5.2
  • platform: CentOS 6.6
  • aiohttp version: 1.0.5

Hi guys,

My program runs normally when using default asyncio policy. But if installing the uvloop event loop policy,

import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

the following exception will be raised:

Traceback (most recent call last):
  File "/opt/venv/websec/lib/python3.5/site-packages/aiohttp/client.py", line 565, in __aenter__
    self._resp = yield from self._coro
  File "/opt/venv/websec/lib/python3.5/site-packages/aiohttp/client.py", line 197, in _request
    with Timeout(timeout, loop=self._loop):
  File "/opt/venv/websec/lib/python3.5/site-packages/async_timeout/__init__.py", line 33, in __enter__
    raise RuntimeError('Timeout context manager should be used '
RuntimeError: Timeout context manager should be used inside a task

Thanks!

@1st1
Copy link
Member

1st1 commented Dec 8, 2016

This is strange, I don't see how this can happen. Could you please share the code so that I can debug this?

@superstarrr
Copy link
Author

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

@asvetlov
Copy link
Contributor

asvetlov commented Dec 9, 2016

Hrrr.

You are creating a global var here (class variable actually).
It is anti-pattern with very bad smell.

@superstarrr
Copy link
Author

I have to admit that it is a bad idea. And will remove these lazy code. Thanks!

@1st1
Copy link
Member

1st1 commented Dec 12, 2016

Should this issue be closed now?

@1st1
Copy link
Member

1st1 commented Dec 16, 2016

OK, closing this.

This issue was closed.
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

3 participants