Home > Software design >  How do I transfer data from an asynchronous loop running in a thread?
How do I transfer data from an asynchronous loop running in a thread?

Time:10-26

import asyncio
import threading

async def asyncio_loop():
    i = 1
    while True:
        i  = 1  # This value must be passed to mainloop()
    

def thread_function():
    asyncio.run(asyncio_loop())

x = threading.Thread(target=thread_function)
x.start()

def mainloop():
    while True:
        pass  # Here I need to get the values from asyncio_loop

mainloop()

I would like to use queues because it is important for me to keep the data ordered.

But for threads I need to use the queue module, but for asyncio there is asyncio.Queue(). Is there any way I can use a shared queue or am I digging in the wrong direction at all?

I don't consider sockets too, because I will be passing the instances of classes. I want to avoid unnecessary complications.

CodePudding user response:

You can use the normal, threading queue.Queue - it will just work, The difference for the async.Queue is that the methods for putting and retrieving data int it are async themselves -but since you will be reading then from synchronous code, that would not work.

On the other hand, calling a synchronous queue.put method in the async code works with no difference whatsoever - since there is no I/O involved it is not even blocking.

import asyncio
import threading

from queue import Queue

async def asyncio_loop(q):
    i = 0
    while True:
        i  = 1  # This value must be passed to mainloop()
        q.put(i)
        await asyncio.sleep(1)
    

def thread_function(q):
    asyncio.run(asyncio_loop(q))

def mainloop(q):
    while True:
        print(q.get())
        
q = Queue()

x = threading.Thread(target=thread_function, args=(q,))
x.start()

mainloop(q)
  • Related