小编Zig*_*igb的帖子

如何使用父子函数收集结果和使用限制

我想要实现的是产生多个父母的东西,每个父母都做一些工作,然后产生一些孩子来检查其他事情并在父母中获取这些结果以做进一步的工作。我还试图制定 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)

python-3.x python-trio

2
推荐指数
1
解决办法
250
查看次数

标签 统计

python-3.x ×1

python-trio ×1