Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PythonAsyncio:在完成一个协程时停止循环_Python_Coroutine_Python Asyncio - Fatal编程技术网

PythonAsyncio:在完成一个协程时停止循环

PythonAsyncio:在完成一个协程时停止循环,python,coroutine,python-asyncio,Python,Coroutine,Python Asyncio,我对python asyncio这个主题还很陌生。我有一个简单的问题: 我有一个包含两个要同时运行的协同程序的任务。第一个协同程序(my_协同程序)只需连续打印某些内容,直到达到第二个到第二个睡眠。第二个协同路由(seq_coroutine)将依次调用其他4个协同路由。我的目标是在seq_协程结束时停止循环,只要它完全完成。确切地说,我希望我的协同程序在seq_协同程序完成之前保持活动状态。有人能帮我吗 我的代码如下: import asyncio async def my_coroutine

我对python asyncio这个主题还很陌生。我有一个简单的问题: 我有一个包含两个要同时运行的协同程序的任务。第一个协同程序(my_协同程序)只需连续打印某些内容,直到达到第二个到第二个睡眠。第二个协同路由(seq_coroutine)将依次调用其他4个协同路由。我的目标是在seq_协程结束时停止循环,只要它完全完成。确切地说,我希望我的协同程序在seq_协同程序完成之前保持活动状态。有人能帮我吗

我的代码如下:

import asyncio

async def my_coroutine(task,  seconds_to_sleep = 3):
    print("{task_name} started\n".format(task_name=task))
    for i in range(1, seconds_to_sleep):
        await asyncio.sleep(1)
        print("\n{task_name}: second {seconds}\n".format(task_name=task, seconds=i))

async def coroutine1():
    print("coroutine 1 started")
    await asyncio.sleep(1)
    print("coroutine 1 finished\n")


async def coroutine2():
    print("coroutine 2 started")
    await asyncio.sleep(1)
    print("coroutine 2 finished\n")


async def coroutine3():
    print("coroutine 3 started")
    await asyncio.sleep(1)
    print("coroutine 3 finished\n")


async def coroutine4():
    print("coroutine 4 started")
    await asyncio.sleep(1)
    print("coroutine 4 finished\n")


async def seq_coroutine():
    await coroutine1()
    await coroutine2()
    await coroutine3()
    await coroutine4()

def main():
    main_loop = asyncio.get_event_loop()
    task = [asyncio.ensure_future(my_coroutine("task1", 11)),
            asyncio.ensure_future(seq_coroutine())]
    try:
        print('loop is started\n')
        main_loop.run_until_complete(asyncio.gather(*task))
    finally:
        print('loop is closed')
        main_loop.close()


if __name__ == "__main__":
    main()
loop is started

task1 started

coroutine 1 started

task1: second 1

coroutine 1 finished
coroutine 2 started

task1: second 2

coroutine 2 finished
coroutine 3 started

task1: second 3

coroutine 3 finished
coroutine 4 started

task1: second 4

coroutine 4 finished

loop is closed
这是该程序的输出:

loop is started

task1 started

coroutine 1 started

task1: second 1

coroutine 1 finished
coroutine 2 started

task1: second 2

coroutine 2 finished
coroutine 3 started

task1: second 3

coroutine 3 finished
coroutine 4 started

task1: second 4

coroutine 4 finished

task1: second 5
task1: second 6
task1: second 7
task1: second 8
task1: second 9
task1: second 10

loop is closed
我只想要这样的东西:

import asyncio

async def my_coroutine(task,  seconds_to_sleep = 3):
    print("{task_name} started\n".format(task_name=task))
    for i in range(1, seconds_to_sleep):
        await asyncio.sleep(1)
        print("\n{task_name}: second {seconds}\n".format(task_name=task, seconds=i))

async def coroutine1():
    print("coroutine 1 started")
    await asyncio.sleep(1)
    print("coroutine 1 finished\n")


async def coroutine2():
    print("coroutine 2 started")
    await asyncio.sleep(1)
    print("coroutine 2 finished\n")


async def coroutine3():
    print("coroutine 3 started")
    await asyncio.sleep(1)
    print("coroutine 3 finished\n")


async def coroutine4():
    print("coroutine 4 started")
    await asyncio.sleep(1)
    print("coroutine 4 finished\n")


async def seq_coroutine():
    await coroutine1()
    await coroutine2()
    await coroutine3()
    await coroutine4()

def main():
    main_loop = asyncio.get_event_loop()
    task = [asyncio.ensure_future(my_coroutine("task1", 11)),
            asyncio.ensure_future(seq_coroutine())]
    try:
        print('loop is started\n')
        main_loop.run_until_complete(asyncio.gather(*task))
    finally:
        print('loop is closed')
        main_loop.close()


if __name__ == "__main__":
    main()
loop is started

task1 started

coroutine 1 started

task1: second 1

coroutine 1 finished
coroutine 2 started

task1: second 2

coroutine 2 finished
coroutine 3 started

task1: second 3

coroutine 3 finished
coroutine 4 started

task1: second 4

coroutine 4 finished

loop is closed

我刚刚为我的问题找到了一个合适的解决办法。 我不会删除我的帖子,我会发布我的解决方案,以帮助其他面临同样问题的人。 我使用了
asyncio.wait(task,return\u when=asyncio.FIRST\u COMPLETED)
,它将在第一个任务完成时返回结果。 这就是解决方案:

import asyncio
from asyncio.tasks import FIRST_COMPLETED
from concurrent.futures import CancelledError

async def my_coroutine(task,  seconds_to_sleep = 3):
    print("{task_name} started\n".format(task_name=task))
    for i in range(1, seconds_to_sleep):
        await asyncio.sleep(1)
        print("\n{task_name}: second {seconds}\n".format(task_name=task, seconds=i))

async def coroutine1():
    print("coroutine 1 started")
    await asyncio.sleep(1)
    print("coroutine 1 finished\n")


async def coroutine2():
    print("coroutine 2 started")
    await asyncio.sleep(1)
    print("coroutine 2 finished\n")


async def coroutine3():
    print("coroutine 3 started")
    await asyncio.sleep(1)
    print("coroutine 3 finished\n")


async def coroutine4():
    print("coroutine 4 started")
    await asyncio.sleep(1)
    print("coroutine 4 finished\n")


async def seq_coroutine(loop):
    await coroutine1()
    await coroutine2()
    await coroutine3()
    await coroutine4()

def main():
    main_loop = asyncio.get_event_loop()
    task = [asyncio.ensure_future(my_coroutine("task1", 11)),
            asyncio.ensure_future(seq_coroutine(main_loop))]
    try:
        print('loop is started\n')
        done, pending = main_loop.run_until_complete(asyncio.wait(task, return_when=asyncio.FIRST_COMPLETED))
        print("Completed tasks: {completed}\nPending tasks: {pending}".format(completed = done, pending = pending))

        #canceling the tasks
        for task in pending:
            print("Cancelling {task}: {task_cancel}".format(task=task, task_cancel=task.cancel()))

    except CancelledError as e:
        print("Error happened while canceling the task: {e}".format(e=e))
    finally:
        print('loop is closed')


if __name__ == "__main__":
    main()

您可以使用一个变量向另一个协程发送信号<代码>异步。通常使用事件:

import asyncio

import random


async def clock(name, event):
    print("* {} started".format(name))
    i = 0
    while not event.is_set():
        await asyncio.sleep(0.1)
        i += 1
        print("* {}: {}".format(name, i))
    print("* {} done".format(name))
    return i


async def coro(x):
    print("coro() started", x)
    await asyncio.sleep(random.uniform(0.2, 0.5))
    print("coro() finished", x)


async def seq_coroutine(name):
    event = asyncio.Event()
    clock_task = asyncio.ensure_future(clock(name, event))
    # await asyncio.sleep(0) # if you want to give a chance to clock() to start
    await coro(1)
    await coro(2)
    await coro(3)
    await coro(4)
    event.set()
    i = await clock_task
    print("Got:", i)


def main():
    main_loop = asyncio.get_event_loop()
    main_loop.run_until_complete(seq_coroutine("foo"))
    main_loop.close()


if __name__ == "__main__":
    main()
您还可以使用
wait event.wait()
阻止一段代码,直到设置事件:

async def xxx(event):
    print("xxx started")
    await event.wait()
    print("xxx ended")

为什么不运行
直到完成(seq\u coroutine)
?同时执行此操作的想法不仅仅是睡觉和打印。“my_Corroutine”是一个倾听其他内容的过程,应该与seq_Corroutine并行运行,但为了让提问更简单,我只是将其缩短。