I have the following two async function
from tornado.httpclient import AsyncHTTPClient
async def get_categories(): # return a list of str
# ....
http = AsyncHTTPClient()
resp = await http.fetch(....)
return [....]
async def get_details(category): # return a list of dict
# ....
http = AsyncHTTPClient()
resp = await http.fetch(....)
return [....]
Now I need to create a function to get the details (run http fetch concurrently) for all the categories and combine them together.
async def get_all_details():
categories = await get_categories()
tasks = list(map(lambda x: {'category': x, 'task':get_details(x)}, categories))
r = await asyncio.gather(*tasks) # error
# need to return [
# {'category':'aaa', 'detail':'aaa detail 1'},
# {'category':'aaa', 'detail':'aaa detail 2'},
# {'category':'bbb', 'detail':'bbb detail 1'},
# {'category':'bbb', 'detail':'bbb detail 2'},
# {'category':'bbb', 'detail':'bbb detail 3'},
# {'category':'ccc', 'detail':'ccc detail 1'},
# {'category':'ccc', 'detail':'aaa detail 2'},
# ]
However, the list line return the error:
TypeError: unhashable type: 'dict'
The tasks has the following values:
[{'category': 'aaa',
'task': <coroutine object get_docker_list at 0x000001B12B8560C0>},
{'category': 'bbb',
'task': <coroutine object get_docker_list at 0x000001B12B856F40>},
{'category': 'ccc',
'task': <coroutine object get_docker_list at 0x000001B12B856740>}]
BTW, is it a way to throttle the http fetch calls? For example, at most four fetches running at the same time.