gpt4 book ai didi

异步函数内的python异步函数

转载 作者:行者123 更新时间:2023-12-05 06:01:24 26 4
gpt4 key购买 nike

我的代码:

import asyncio
from random import randrange

async def inner_sleep(letter, num):
print(f'start inner sleep {letter}, {num}')
myint = randrange(5)
await asyncio.sleep(myint)
print(f'done with inner sleep {letter}, {num}')

async def outer_sleep(letter):
print(f'start outer sleep {letter}')
myint = randrange(5)
await asyncio.sleep(myint)
print(f'done with outer sleep {letter}')

async def inside(letter):
nums = [1,2,3,4,5,6]
tasks = []

async def create_task(num):
task = asyncio.ensure_future(inner_sleep(letter, num))
tasks.append(task)

for num in nums:
await create_task(num)

await asyncio.gather(*tasks)


async def outside():

letters = ['a','b','c','d']
tasks = []

async def create_task_1(letter):
task = asyncio.ensure_future(outer_sleep(letter))
tasks.append(task)

for letter in letters:
await create_task_1(letter)
await inside(letter)

await asyncio.gather(*tasks)

asyncio.run(outside())

示例输出:

start outer sleep a
start inner sleep a, 1
start inner sleep a, 2
start inner sleep a, 3
start inner sleep a, 4
start inner sleep a, 5
start inner sleep a, 6
done with inner sleep a, 4
done with outer sleep a
done with inner sleep a, 2
done with inner sleep a, 3
done with inner sleep a, 1
done with inner sleep a, 5
done with inner sleep a, 6
start outer sleep b
start inner sleep b, 1
start inner sleep b, 2
start inner sleep b, 3
start inner sleep b, 4
start inner sleep b, 5
start inner sleep b, 6
done with inner sleep b, 3
done with inner sleep b, 5
done with inner sleep b, 4
done with outer sleep b
done with inner sleep b, 1
done with inner sleep b, 6
done with inner sleep b, 2
start outer sleep c
start inner sleep c, 1

我在一个循环中有一个循环,我希望它们都异步运行。内部的工作正常,但我无法让外部的做我想做的事。在上面的代码中,我希望我的外部函数同时循环遍历列表“字母”。对于每个字母,我需要第一个函数 (outer_sleep) 在第二个函数 (inner_sleep) 开始之前完成。我似乎找不到放置内部函数来完成此操作的地方。当我运行这段代码时,您可以在结果中看到“内部 sleep a”是如何在“完成外部 sleep a”之前开始的。理想情况下,我的输出类似于:

start outer sleep a
start outer sleep b
finish outer sleep a
start inner sleep a, 1
start inner sleep a, 2
start inner sleep a, 3
start inner sleep a, 4
start inner sleep a, 5
start inner sleep a, 6
finish outer sleep b
start inner sleep b, 1
start inner sleep b, 2
start inner sleep b, 3
start inner sleep b, 4
start inner sleep b, 5
start inner sleep b, 6
done with inner sleep a, 4
done with inner sleep a, 2
done with inner sleep b, 3

这可能吗?

最佳答案

await inside(letter) 放在 outer_sleep 的末尾怎么样?这确保它只会在特定 outer_sleep 的结果到达时运行。

import asyncio
from random import randrange


async def inner_sleep(letter, num):
print(f'start inner sleep {letter}, {num}')
myint = randrange(5)
await asyncio.sleep(myint)
print(f'done with inner sleep {letter}, {num}')


async def inside(letter):
nums = [1, 2, 3, 4, 5, 6]
tasks = [asyncio.ensure_future(inner_sleep(letter, num)) for num in nums]
await asyncio.gather(*tasks)


async def outer_sleep(letter):
print(f'start outer sleep {letter}')
myint = randrange(5)
await asyncio.sleep(myint)
print(f'done with outer sleep {letter}')
await inside(letter)


async def outside():
letters = ['a', 'b', 'c', 'd']
tasks = [asyncio.ensure_future(outer_sleep(letter)) for letter in letters]
await asyncio.gather(*tasks)


asyncio.run(outside())

asyncio.gather 意味着数字和字母都可以按任何顺序处理(例如,'c' 可以在 'b' 之前),如果您愿意的话。

关于异步函数内的python异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67227498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com