我有以下代码,这些代码将在http端点上执行GET请求。但是,一次只做一次就太慢了。因此,下面的代码一次将它们处理50个,但是我需要将它们添加到集合中(我认为集合会是最快的,因为此脚本将返回重复的对象。现在,这只是返回对象中的对象一次将一个字符串50分开,以便在它们全部组合后可以对其进行排序。我是python的新手,所以我不确定还有什么尝试
import asyncio
from aiohttp import ClientSession
async def fetch(url, session):
async with session.get(url) as response:
return await response.read()
async def run(r):
url = "http://httpbin.org/get"
tasks = []
# Fetch all responses within one Client session,
# keep connection alive for all requests.
async with ClientSession() as session:
for i in range(r):
task = asyncio.ensure_future(fetch(url.format(i), session))
tasks.append(task)
responses = await asyncio.gather(*tasks)
# you now have all response bodies in this variable
print(responses)
def print_responses(result):
print(result)
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(20))
loop.run_until_complete(future)
现在,它只是将所有请求响应转储到result
,我需要它将每个响应添加到集合中,以便以后可以使用数据