[Python] Communication between async tasks and synchronous threads in python

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am looking for the best solution for communication between async tasks and methods/functions that run in a thread pool executor from concurrent.futures. In previous synchronous projects, I would use the queue.Queue class. I assume that any method should be thread safe and therefore the asyncio.queue will not work.

    I have seen people extend the queue.Queue class to do something like:

    class async_queue(Queue):
    async def aput(self, item):
    self.put_nowait(item)

    async def aget(self):
    resp = await asyncio.get_event_loop().run_in_executor( None, self.get )
    return resp


    Is there a better way?

    Continue reading...

Compartilhe esta Página