[Python] How to communicate between traditional thread and asyncio thread in Python?

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    In python, what's the idiomatic way to establish a one-way communication between two threading.Threads, call them thread a and thread b.

    a is the producer, it continuously generates values for b to consume.

    b is the consumer, it reads one value generated by a, process the value with a coroutine, and then reads the next value, and so on.

    Illustration:

    q = very_magic_queue.Queue()


    def worker_of_a(q):
    while True:
    q.put(1)
    time.sleep(1)

    a = threading.Thread(worker_of_a, args=(q,))
    a.start()


    async def loop(q):
    while True:
    # v must be processed in the same order as they are produced
    v = await q.get()
    print(v)

    async def foo():
    pass

    async def b_main(q):
    loop_fut = asyncio.ensure_future(loop(q))
    foo_fut = asyncio.ensure_future(foo())
    _ = await asyncio.wait([loop_fut, foo_fut], ...)
    # blah blah blah

    def worker_of_b(q):
    asyncio.set_event_loop(asyncio.new_event_loop())
    asyncio.get_event_loop().run_until_complete(b_main(q))

    b = threading.Thread(worker_of_b, args=(q,))
    b.start()


    Of course the above code doesn't work, because queue.Queue.get cannot be awaitted, and asyncio.Queue cannot be used in another thread.

    I also need a communication channel from b to a.

    I would be great if the solution could also work with gevent.

    Thanks :)

    Continue reading...

Compartilhe esta Página