在与cocotb的列表中产生一个协程

2024-09-30 08:24:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个协同程序,等待事件设置:

@cocotb.coroutine
def wb_RXDR_read(self):
    """ waiting util RXDR is read """
    if not self._RXDR_read_flag:
        while True:
            yield self._RXDR_read_event.wait()
            break

我想在它上面«屈服»超时。为了做到这一点,我做了:

        RXDR_timeout = Timer(250, units="us")
        ret = yield [RXDR_timeout, self.wb_RXDR_read()]
        if ret == RXDR_timeout:
            self._dut._log.error("Timeout on waiting RXDR to be read")
            raise TestError()

但我有个错误:

2ns ERROR    Coroutine i2c_write yielded something the scheduler can't handle
                      Got type: <type 'list'> repr: [<cocotb.triggers.Timer object at 0x7f2098cb1350>, <cocotb.decorators.RunningCoroutine object at 0x7f2098cb1610>] str: [<cocotb.triggers.Timer object at 0x7f2098cb1350>, <cocotb.decorators.RunningCoroutine object at 0x7f2098cb1610>]
                      Did you forget to decorate with @cocotb.coroutine?

我的团队装饰有@协同程序. 如果我一个人放弃,那就行了:

yield self.wb_RXDR_read() # <- that works

但我不能把它列在清单上。有没有可能像unix select()那样将协程放在一个要阻止的列表中?还是保留触发类?你知道吗


Tags: selfreadifobjecttimeoutattimeryield
1条回答
网友
1楼 · 发布于 2024-09-30 08:24:34

好的,我找到了解决办法。事实上,协同程序不能像时装本身那样被触发。它应该首先作为一个线程启动,并且为了检测协同程序的结束,.join()方法必须放在yield列表中:

    RXDR_timeout = Timer(250, units="us")
    RXDR_readth = cocotb.fork(self.wb_RXDR_read())
    ret = yield [RXDR_timeout, RXDR_readth.join()]
    if ret == RXDR_timeout:
        self._dut._log.error("Timeout on waiting RXDR to be read")
        raise TestError()

要记住的是:

  • 我们可以产生一个协同程序
  • 为了产生多个协程,我们必须fork()join()

相关问题 更多 >

    热门问题