简体   繁体   English

如何从字典列表中收集任务 aiohttp async python

[英]How to gather tasks from a list of dictionaries aiohttp async python

I have the following list of dictionary:我有以下字典列表:

[{'id': 1, 'url': 'https://url1.com'}, {'id': 2, 'url': 'https://url2.com'} , {'id': 3, 'url': 'https://url3.com'}]

How to use asyncio.gather to pass id and url to a sync method ?如何使用asyncio.gatheridurl传递给sync method

ideally something like this:理想情况下是这样的:

asyncio.get_event_loop().run_until_complete(
        asyncio.gather(*[process(id, url)])) # process is another async method which will need id and url separately

if I just had a list of url then I know I could easily do something like this:如果我只有 url 的list ,那么我知道我可以轻松地做这样的事情:

asyncio.get_event_loop().run_until_complete(
        asyncio.gather(*[process(url) for url in url_list]))

tvm电视

A list comprehension would produce your list of URLs.列表推导会生成您的 URL 列表。 Something like:就像是:

x = [{'id': 1, 'url': 'https://url1.com'}, {'id': 2, 'url': 'https://url2.com'} , {'id': 3, 'url': 'https://url3.com'}]
urls = [a_dict['url'] for a_dict in x]

If you want id and url then extract them as a list of tuples:如果你想要idurl然后将它们提取为元组列表:

 [ (a_dict['id'], a_dict['url']) for a_dict in x]
 >>>> [(1, 'https://url1.com'), (2, 'https://url2.com'), (3, 'https://url3.com')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM