In the documentation for aiohttp.ClientSession I have read that you should, ideally, create one ClientSession per application. I suspect that that should read something like "create one ClientSession per thread" (I'm in python 3x).
import aiohttp
import asyncio
async def get_it_async(url: str, session):
async with session.get(url) as resp:
print(resp.status)
print(len(await resp.text()))
def run_it(fn, *args, **kwargs):
# New thread - get the loop.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
assert not loop.is_running()
return loop.run_until_complete(loop.create_task(fn(*args, **kwargs)))
from concurrent.futures import ThreadPoolExecutor
pool = ThreadPoolExecutor(2)
client = aiohttp.ClientSession()
await asyncio.wrap_future(pool.submit(run_it, get_it_async, 'httyp://nytimes.com', client))
Gives me:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-1-180a408a9698> in async-def-wrapper()
~\appdata\Local\Programs\Python\Python37\lib\concurrent\futures\thread.py in run(self)
55
56 try:
---> 57 result = self.fn(*self.args, **self.kwargs)
58 except BaseException as exc:
59 self.future.set_exception(exc)
<ipython-input-1-180a408a9698> in run_it(fn, *args, **kwargs)
15
16
---> 17 from concurrent.futures import ThreadPoolExecutor
18 pool = ThreadPoolExecutor(2)
19
~\appdata\Local\Programs\Python\Python37\lib\asyncio\base_events.py in run_until_complete(self, future)
585 raise RuntimeError('Event loop stopped before Future completed.')
586
--> 587 return future.result()
588
589 def stop(self):
<ipython-input-1-180a408a9698> in get_it_async(url, session)
6 print(resp.status)
7 print(len(await resp.text()))
----> 8
9 def run_it(fn, *args, **kwargs):
10 # New thread - get the loop.
i:\gwatts\code\calratio2019\calms\.venv\lib\site-packages\aiohttp\client.py in __aenter__(self)
1010
1011 async def __aenter__(self) -> _RetType:
-> 1012 self._resp = await self._coro
1013 return self._resp
1014
i:\gwatts\code\calratio2019\calms\.venv\lib\site-packages\aiohttp\client.py in _request(self, method, str_or_url, params, data, json, cookies, headers, skip_auto_headers, auth, allow_redirects, max_redirects, compress, chunked, expect100, raise_for_status, read_until_eof, proxy, proxy_auth, timeout, verify_ssl, fingerprint, ssl_context, ssl, proxy_headers, trace_request_ctx)
424 timer = tm.timer()
425 try:
--> 426 with timer:
427 while True:
428 url, auth_from_url = strip_auth_from_url(url)
i:\gwatts\code\calratio2019\calms\.venv\lib\site-packages\aiohttp\helpers.py in __enter__(self)
577
578 if task is None:
--> 579 raise RuntimeError('Timeout context manager should be used '
580 'inside a task')
581
RuntimeError: Timeout context manager should be used inside a task
Is there a bug in my code, or is my assumption correct?