python 3 asyncio:使用run_until_complete(asyncio.wait(corutines_list))协程执行顺序 [英] python 3 asyncio: coroutines execution order using run_until_complete(asyncio.wait(corutines_list))

查看:171
本文介绍了python 3 asyncio:使用run_until_complete(asyncio.wait(corutines_list))协程执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可能毫无用处的问题,但是我仍然觉得我缺少一些对于理解asyncio的工作方式可能很重要的东西.

I have what is probably a quite useless question, but nevertheless I feel I am missing something that might be important to understand how asyncio works.

我刚刚开始熟悉asyncio,我写了这段非常基本的代码:

I just started to familiarize with asyncio and I wrote this very basic piece of code:

import asyncio
import datetime
from random import randint


async def coroutine(i):
    start = datetime.datetime.now()
    print('coroutine {} started.'.format(i))
    n = randint(1, 11)
    await asyncio.sleep(n)
    end = datetime.datetime.now()
    print('coroutine {} finished after {} with random = {}.'.format(i, (end-start).seconds, n))
    return i


def simple():
    loop = asyncio.get_event_loop()
    cors = [coroutine(x) for x in range(20)]
    loop.run_until_complete(asyncio.wait(cors))


if __name__ == '__main__':
    simple()

这是我得到的结果:

coroutine 3 started.
coroutine 9 started.
coroutine 15 started.
coroutine 4 started.
coroutine 10 started.
coroutine 16 started.
coroutine 1 started.
coroutine 5 started.
coroutine 11 started.
coroutine 17 started.
coroutine 2 started.
coroutine 6 started.
coroutine 12 started.
coroutine 18 started.
coroutine 0 started.
coroutine 7 started.
coroutine 13 started.
coroutine 19 started.
coroutine 8 started.
coroutine 14 started.
coroutine 7 finished after 1 with random = 1.
coroutine 12 finished after 2 with random = 2.
coroutine 3 finished after 3 with random = 3.
coroutine 5 finished after 3 with random = 3.
coroutine 0 finished after 3 with random = 3.
coroutine 10 finished after 4 with random = 4.
coroutine 17 finished after 4 with random = 4.
coroutine 2 finished after 5 with random = 5.
coroutine 16 finished after 6 with random = 6.
coroutine 18 finished after 6 with random = 6.
coroutine 15 finished after 7 with random = 7.
coroutine 9 finished after 8 with random = 8.
coroutine 1 finished after 8 with random = 8.
coroutine 6 finished after 8 with random = 8.
coroutine 11 finished after 9 with random = 9.
coroutine 8 finished after 9 with random = 9.
coroutine 4 finished after 10 with random = 10.
coroutine 13 finished after 10 with random = 10.
coroutine 19 finished after 10 with random = 10.
coroutine 14 finished after 10 with random = 10.

现在,我的问题是:为什么协程从一个混乱的顺序开始?我原本希望看到从协程0到协程20的有序协程x开始"消息...直到那时我才认为由于随机的睡眠时间它们会争先恐后...我想念什么?

Now, my question is: why on earth the coroutines start in a scrambled order? I was expecting to see an ordered "coroutine x started" message, from coroutine 0 to coroutine 20...only then I assumed they would scramble due to the randomized sleep time...what am I missing?

推荐答案

根据 .wait()规范,顺序不确定.

The order is undetermenistic by .wait() specification.

您不应该在意它.

实际上,所有协程几乎都是在同一循环迭代中启动的.

Actually all coroutines are started at the same loop iteration, almost at the same time.

这篇关于python 3 asyncio:使用run_until_complete(asyncio.wait(corutines_list))协程执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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