Issue
I am currently using the following code to query an api for data
import asyncio
async def get_data(session, ticker):
while True:
try:
async with session.get(url=f'api.exec.com') as response:
if response.status == 200:
data = await response.json()
try:
df = pd.DataFrame(data)
except ValueError:
print("Data is not a valid float. Retrying...")
else:
print("Received non-200 status code. Retrying...")
await asyncio.sleep(1)
except Exception as e:
print("Unable to get url {} due to {}. Retrying...".format(ticker, e.__class__))
await asyncio.sleep(1)
async def main_loop(ticker_list):
async with aiohttp.ClientSession() as session:
ret = await asyncio.gather(*[get_data(session, tick) for tick in ticker_list])
return ret
datas = ['x1', 'x2', 'x3', 'x4']
results = asyncio.run(main(datas))
print(results)
I want to add a timeout to the session.get.
If i was using a normal python request, I can just do the following
session.get(url=f'api.exec.com', timeout=5)
or
session.get(url=f'api.exec.com', timeout=(4,7))
How do I do the same thing with asyncio / aiohttp.ClientSession?
Should I be doing, where I just add timeout = 5 into the seesion.get line?
import asyncio
async def get_data(session, ticker):
while True:
try:
async with session.get(url=f'api.exec.com', timeout = 5) as response:
if response.status == 200:
data = await response.json()
try:
df = pd.DataFrame(data)
except ValueError:
print("Data is not a valid float. Retrying...")
else:
print("Received non-200 status code. Retrying...")
await asyncio.sleep(1)
except Exception as e:
print("Unable to get url {} due to {}. Retrying...".format(ticker, e.__class__))
await asyncio.sleep(1)
async def main_loop(ticker_list):
async with aiohttp.ClientSession() as session:
ret = await asyncio.gather(*[get_data(session, tick) for tick in ticker_list])
return ret
datas = ['x1', 'x2', 'x3', 'x4']
results = asyncio.run(main(datas))
print(results)
Or should I be doing this? Where I add a timeout=aiohttp.ClientTimeout(total=5) object into the timeout?
import asyncio
async def get_data(session, ticker):
while True:
try:
async with session.get(url=f'api.exec.com', timeout=aiohttp.ClientTimeout(total=5)) as response:
if response.status == 200:
data = await response.json()
try:
df = pd.DataFrame(data)
except ValueError:
print("Data is not a valid float. Retrying...")
else:
print("Received non-200 status code. Retrying...")
await asyncio.sleep(1)
except Exception as e:
print("Unable to get url {} due to {}. Retrying...".format(ticker, e.__class__))
await asyncio.sleep(1)
async def main_loop(ticker_list):
async with aiohttp.ClientSession() as session:
ret = await asyncio.gather(*[get_data(session, tick) for tick in ticker_list])
return ret
datas = ['x1', 'x2', 'x3', 'x4']
results = asyncio.run(main(datas))
print(results)
or like this? where I set the timeout in the main function?
import asyncio
async def get_data(session, ticker):
while True:
try:
async with session.get(url=f'api.exec.com') as response:
if response.status == 200:
data = await response.json()
try:
df = pd.DataFrame(data)
except ValueError:
print("Data is not a valid float. Retrying...")
else:
print("Received non-200 status code. Retrying...")
await asyncio.sleep(1)
except Exception as e:
print("Unable to get url {} due to {}. Retrying...".format(ticker, e.__class__))
await asyncio.sleep(1)
async def main_loop(ticker_list):
timeout = aiohttp.ClientTimeout(total=5) # Setting timeout here
async with aiohttp.ClientSession(timeout=timeout) as session:
ret = await asyncio.gather(*[get_data(session, tick) for tick in ticker_list])
return ret
datas = ['x1', 'x2', 'x3', 'x4']
results = asyncio.run(main(datas))
print(results)
Solution
timeout = 5
This isn't valid according to the documentation, and won't work at all in v4.
The other 2 options you gave are correct.
The first sets the timeout for each request individually (and will override the timeout set in the ClientSession).
The second sets it in the session globally, so it automatically applies to all requests in that session (if not overriden).
Answered By - Sam Bull
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.