Kill a coroutine along with all the child coroutines that it has forked (feature request)
See original GitHub issueI find that it is often useful to kill a coroutine and also at the same time kill all the coroutines that it has forked.
A typical example is if I have a testing coroutine that takes care of forking all the stimulus coroutines and checking coroutines. In the main test function I fork the clock, send a reset signal and then fork the testing coroutine. The main test then waits for some arbitrary number of clock cycles before resetting again, killing the testing coroutine and all it’s child coroutines, and then forking another testing coroutine. In this way I can test that the reset is working well interrupting the module at an arbitrary state.
async def do_test(dut):
fork(send_stimulus(dut))
await check_output(dut)
@cocotb.test():
def top_level_test(dut)
seed = 0
n_resets = 10
p_reset = 0.001
do_test_task = None
rnd = random.Random(seed)
fork(start_clock(dut))
for reset_index in range(n_resets):
await triggers.RisingEdge(dut.clk)
if do_test_task is not None:
do_test_task.kill()
dut.reset <= 1
await triggers.RisingEdge(dut.clk)
dut.reset <= 0
do_test_task = fork(do_test(dut))
while True:
if rnd.random() < p_reset:
break
await RisingEdge(dut.clk)
At the moment can’t just kill the coroutine, rather I also need to manually kill all the children that the coroutine has created with fork
or await
. Currently I’m taking care of this with a hackish solution (https://github.com/benreynwar/slvcodec/blob/master/slvcodec/cocotb_helper.py), but it would be really nice if this was something that the scheduler could take care of itself. I’m also curious about how other people are solving these kinds of issues.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
I had a crack at implementing something like that in https://github.com/benreynwar/slvcodec/blob/master/slvcodec/event.py#L493
In asyncio this would be achieved with
try/finally
blocks to clean up after the cancellation error. Throwing a similar error I’m cocotb after akill
would allow the same pattern.