简体   繁体   English

如何使用 asyncio.gather 立即解析响应?

[英]how to parse response immediately with asyncio.gather?

async def main():
    uuids = await get_uuids_from_text_file()
    tasks = []
    # create a task for each uuid
    # and add it to the list of tasks
    for uuid in uuids:
        task = asyncio.create_task(make_hypixel_request(uuid))
        tasks.append(task)
    # wait for all the tasks to finish
    responses = await asyncio.gather(*tasks)
    # run the functions to process the data
    for response in responses:
        print(response['success'])
        data2 = await anti_sniper_request(response)
        await store_data_in_json_file(response, data2)
        await compare_stats(response)

# loop the main function
async def main_loop():
    for _ in itertools.repeat([]):
        await main()


# run the loop
loop = asyncio.get_event_loop()
loop.run_until_complete(main_loop())
loop.close()

basically this is my code the functions have very clear and explanatory name the make_hypixel_requests part i have no issues there, the requests are executed immediately and in parallel, the problem is after that when "for response in responses" it goes hella slow?基本上这是我的代码函数有非常清晰和解释性的名称 make_hypixel_requests 部分我在那里没有问题,请求立即并行执行,问题是在那之后当“响应中的响应”变得很慢时? how do i get the responses instantly and loop through them very fast?我如何立即获得响应并快速循环? i will try to attach a gif.我会尝试附加一个 gif。

basically this is the issue:基本上这是个问题: 在此处输入图像描述

the reason is because after waiting for all the responses to return, u process them in a loop instead of asynchronously, since none of the requests seem to depend on each other, waiting for them all to finish before processing them doesn't make sense, the best way to handle this is to couple the request and the processing eg原因是因为在等待所有响应返回之后,你在循环中而不是异步地处理它们,因为没有一个请求似乎相互依赖,等待它们全部完成再处理它们没有意义,处理这个问题的最好方法是将请求和处理结合起来,例如

async def request_and_process(uuid):
    response = await make_hypixel_request(uuid)
    print(response['success'])
    compare_stats_task = asyncio.create_task(compare_stats(response))
    data2 = await anti_sniper_request(response)
    await asyncio.gather(store_data_in_json_file(response, data2), compare_stats_task)

async def main():
    while True:
        uuids = await get_uuids_from_text_file()
        await asyncio.gather(*map(request_and_process, uuids))

asyncio.run(main())

You can use asyncio.wait and return when at least a task is completed, then continue awaiting for the pending tasks.您可以使用asyncio.wait并在至少完成一项任务时返回,然后继续等待挂起的任务。 asyncio.wait return a tuple with two sets, the first with the completed tasks, the second with the still pending tasks. asyncio.wait返回一个包含两组的元组,第一个包含已完成的任务,第二个包含仍未完成的任务。 You can call to the result method of the done tasks and get its return value.您可以调用已完成任务的result方法并获取其返回值。

async def main():
    uuids = await get_uuids_from_text_file()
    tasks = []
    # create a task for each uuid
    # and add it to the list of tasks
    for uuid in uuids:
        task = asyncio.create_task(make_hypixel_request(uuid))
        tasks.append(task)
    # wait for all the tasks to finish
    while tasks:
        done_tasks, tasks = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
        for done in done_tasks:
            response = done.result()
            print(response['success'])
            data2 = await anti_sniper_request(response)
            await store_data_in_json_file(response, data2)
            await compare_stats(response)

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

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