如何在另一个异步cio循环内使用异步cio循环 [英] How to use an asyncio loop inside another asyncio loop

查看:31
本文介绍了如何在另一个异步cio循环内使用异步cio循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试各种方法,以便能够在另一个异步循环中使用异步循环。大多数情况下,我的测试只是以错误结束,例如:

运行错误:此事件循环已在运行

我下面的示例代码只是我开始的基础测试,因此您可以看到我要做的事情的基本情况。这次考试后我尝试了太多的东西,太让人困惑了,所以我想我应该在寻求帮助的时候保持简单。如果有人能给我指出正确的方向,那就太好了。感谢您抽出时间!

import asyncio

async def fetch(data):
    message = 'Hey {}!'.format(data)
    other_data = ['image_a.com', 'image_b.com', 'image_c.com']
    images = sub_run(other_data)
    return {'message' : message, 'images' : images}

async def bound(sem, data):
    async with sem:
        r = await fetch(data)
        return r

async def build(dataset):
    tasks = []
    sem = asyncio.Semaphore(400)

    for data in dataset:
        task = asyncio.ensure_future(bound(sem, data))
        tasks.append(task)

    r = await asyncio.gather(*tasks)
    return r

def run(dataset):
    loop = asyncio.get_event_loop()
    future = asyncio.ensure_future(build(dataset))
    responses = loop.run_until_complete(future)
    loop.close()
    return responses

async def sub_fetch(data):
    image = 'https://{}'.format(data)
    return image

async def sub_bound(sem, data):
    async with sem:
        r = await sub_fetch(data)
        return r

async def sub_build(dataset):
    tasks = []
    sem = asyncio.Semaphore(400)

    for data in dataset:
        task = asyncio.ensure_future(sub_bound(sem, data))
        tasks.append(task)

    r = await asyncio.gather(*tasks)
    return r

def sub_run(dataset):
    loop = asyncio.get_event_loop()
    future = asyncio.ensure_future(sub_build(dataset))
    responses = loop.run_until_complete(future)
    loop.close()
    return responses

if __name__ == '__main__':
    dataset = ['Joe', 'Bob', 'Zoe', 'Howard']
    responses = run(dataset)
    print (responses)

推荐答案

在运行事件循环内运行loop.run_until_compete会挡路外部循环,从而违背使用异步CIO的目的。正因为如此,异步事件循环不是递归的,应该不需要递归地运行它们。await在现有事件循环上执行任务,而不是创建内部事件循环。

在您的情况下,删除sub_run,只需替换其用法:

images = sub_run(other_data)

使用:

images = await sub_build(other_data)

它将工作得很好,运行子协同例程,并且在内部协程完成之前不会继续执行外部协程,这很可能是您从同步代码中想要的。

这篇关于如何在另一个异步cio循环内使用异步cio循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆