File "C:/Users/user/PycharmProjects/untitled/main.py", line 32, in fetch_async
response = await aiohttp.request('GET', URL)
TypeError: object _SessionRequestContextManager can't be used in 'await' expression
import time
import asyncio
import aiohttp
URL = 'https://api.github.com/events'
MAX_CLIENTS = 1
async def fetch_async(pid):
print('Fetch async process {} started'.format(pid))
start = time.time()
response = await aiohttp.request('GET', URL)
datetime = response.headers.get('Date')
print('Process {}: {}, took: {:.2f} seconds'.format(
pid, datetime, time.time() - start))
response.close()
return datetime
async def asynchronous():
start = time.time()
tasks = [asyncio.ensure_future(
fetch_async(i)) for i in range(1, MAX_CLIENTS + 1)]
await asyncio.wait(tasks)
print("Process took: {:.2f} seconds".format(time.time() - start))
ioloop = asyncio.get_event_loop()
ioloop.run_until_complete(asynchronous())
ioloop.close()
Python 3.6.5 (default, Mar 29 2018, 03:28:50)
[GCC 5.4.0 20160609] on linux
aiohttp==3.2.1
import asyncio
import logging
import sys
import time
import aiohttp
logger = logging.getLogger('aiohttp_test')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
URL = 'https://api.github.com/events'
MAX_CLIENTS = 2
async def fetch_async(session, pid):
logger.info(f'Fetch async process {pid} started')
start = time.time()
async with session.get(URL) as response:
datetime = response.headers.get('Date')
logger.info(f'Process {pid}: {datetime}, took: {time.time() - start} seconds')
return datetime
async def asynchronous():
start = time.time()
async with aiohttp.ClientSession() as session:
tasks = [asyncio.ensure_future(
fetch_async(session, pid)) for pid in range(1, MAX_CLIENTS + 1)]
await asyncio.gather(*tasks)
logger.info(f'Process took: {time.time() - start} seconds')
if __name__ == '__main__':
io_loop = asyncio.get_event_loop()
try:
logger.info('Script has been started')
io_loop.run_until_complete(asynchronous())
except Exception as e:
logger.exception(e)
finally:
logger.info('Script has been finished')
io_loop.close()