The example below is only with 1 element being added to the event loop, with the variable asins.
I am getting a asyncio.exceptions.TimeoutError error when the asins parameter below is 180 elements or longer.
If I create a list with any of these 180 elements, I get a successful response, which tells me the issue below is not related to the API.
Can anyone tell me how to fix this?? thank you!
import asyncio
import aiohttp
import sys
import pandas as pd
def create_params(asins_set):
params = []
for asin in asins_set:
param = {
'api_key': '...',
'type': 'product',
'amazon_domain': 'amazon.com',
'asin': asin,
}
params.append(param)
return params
if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# creates a list of tasks to add to the event loop at once
def get_tasks(session, params):
tasks = []
for param in params:
tasks.append(session.get(
'https://api.rainforestapi.com/request',
params = param
))
return tasks
results = []
async def get_suggested(params):
async with aiohttp.ClientSession() as session:
tasks = get_tasks(session, params)
responses = await asyncio.gather(*tasks)
for response in responses:
results.append(await response.json())
return results
def get_asin_titles(asins_set):
params = create_params(asins_set)
r = asyncio.run(get_suggested(params))
asins_and_titles = dict()
for result in r:
if result['request_info']['success'] == True:
asin = result['request_parameters']['asin']
title = result['product']['title']
asins_and_titles[asin] = title
return asins_and_titles
asins = ['b07wp7q5bf']
final = get_asin_titles(asins)
print(final)