我想要实现的是产生多个父母的东西,每个父母都做一些工作,然后产生一些孩子来检查其他事情并在父母中获取这些结果以做进一步的工作。我还试图制定 2 个不同的重生限制,因为父母的工作可以做的比孩子多。
我将如何做到这一点?
如果我不使用 limit2,它会起作用,但我想要两个限制器。
import trio
import asks
import time
import random
async def child(parent, i, sender, limit2):
async with limit2:
print('Parent {0}, Child {1}: started! Sleeping now...'.format(parent, i))
#await trio.sleep(random.randrange(0, 3))
print('Parent {0}, Child {1}: exiting!'.format(parent, i))
async with sender:
await sender.send('Parent {0}, Child {1}: exiting!'.format(parent, i))
async def parent(i, limit):
async with limit:
print('Parent {0}: started! Sleeping now...'.format(i))
#await trio.sleep(random.randrange(0, 3))
sender, receiver = trio.open_memory_channel(10)
limit2 = trio.CapacityLimiter(2)
async with trio.open_nursery() as nursery:
for j in …Run Code Online (Sandbox Code Playgroud)