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
Reusing aiohttp.ClientSession #391
Comments
What version of Python are you using? I was experiencing a similar problem on 3.5 but after upgrading to 3.6 and not passing the loop explicitly it seemed to resolve itself. |
I was getting the same error (on 3.6 too). |
Thanks @ctlaltdefeat, I solved the issue with the following setup: handlers.py: from sanic.views import HTTPMethodView
from sanic.response import json
async def fetch(session, url):
async with session.get(url) as response:
return await response.json()
class TestView(HTTPMethodView):
def __init__(self, session=None):
self.session = session
async def get(self, request):
url = "https://api.github.com/repos/channelcat/sanic"
response = await fetch(self.session, url)
return json(response) app.py: import aiohttp
from sanic import Sanic
from handlers import TestView
def init(app, loop):
session = aiohttp.ClientSession(loop=loop)
app.add_route(TestView.as_view(session=session), '/')
app = Sanic()
app.run(
host='0.0.0.0',
port=8000,
before_start=init,
) |
Hi,
Using Sanic as a frontend on my API server, I managed to reuse a aiohttp.ClientSession instance by passing it as an argument to Sanic HTTPMethodView subclasses with sanic 0.2.0. I also passed the loop explicitly.
However, with Sanic 0.3.0 I get the following error: 'Timeout context manager should be used inside a task'
Apparently, I have to fall back to instantiating the ClientSession on each request, as in the aiohttp_example.py.
Is there any recommended way to reuse the ClientSession object across requests, since it is quite expensive to initialise them?
Thanking you in advance,
Wouter
The text was updated successfully, but these errors were encountered: