多个asyncio读取器和一个写入器(不同线程)的效率

时间:2019-02-02 23:05:22

标签: python multithreading python-asyncio

我想将线程和异步与一些同步结合起来。

例如:线程将来自摄像机的帧写合并到某个变量或缓冲区中。每次写入都会唤醒多个读取器(异步或线程)以获取最新的可用帧。

我尝试从asyncio.Event派生出来。

class EventThreadSafe(asyncio.Event):
    def set(self):
        self._loop.call_soon_threadsafe(super().set)

是否已经有一种机制(https://github.com/aio-libs/janus?)或如何最好地实现它?

1 个答案:

答案 0 :(得分:0)

您可以使用compiler options definition,但是它不是线程安全的,只是任务安全的。如果您想安全使用线程asyncio.Queue,但这不是任务安全的,因为它将阻塞您的线程。就多进程而言,我个人使用queue.Queue,它向asyncio.Queue适配器提供/从中提供。