Python: Asyncio

import asyncio
import random

async def pull_data(tag, t):
    while True:
        print(tag)
        await asyncio.sleep(random.randint(0, 1))

async def data():
    tasks1 = []
    tasks2 = []
    for i in ['A', 'B', 'C', 'D', 'E']:
        tasks1.append(pull_data("{}1".format(i), 1))
        tasks2.append(pull_data("{}2".format(i), 1))

    #await asyncio.wait(tasks)
    group1 = asyncio.gather(*tasks1)
    group2 = asyncio.gather(*tasks2)
    await asyncio.gather(group1, group2)

if __name__ == '__main__':
    asyncio.run(data())

之前看了官方的例子,也查询了一些博客,后来想到这个例子最言简意赅。
希望对于理解有帮助。

你可能感兴趣的:(Python: Asyncio)