Before you tell me to look at this link, please know I have already tried that approach and I can't understand it. Maybe I'm using it wrong, but it didn't work for me.
import threading
import asyncio
from collections import deque
import queue
f = queue.Queue()
nq = deque()
loop = asyncio.get_event_loop()
async def t():
return 5
def func():
f.put(t())
func()
class T(threading.Thread):
def func(self, value):
fut = asyncio.run_coroutine_threadsafe(value, loop)
nq.append(fut)
print(nq) # deque([<Future at 0x3afa0a0 state=pending>])
def run(self):
while True:
try:
x = f.get(0.2)
print(x) # <coroutine object t at 0x03AF5068>
except:
continue
self.func(x)
T().start()
while True:
try:
val = nq.pop()
break
except IndexError:
continue
print(val) # <Future at 0x3afa0a0 state=pending>
# I'm not even bothering to call `val.result()` because I know the future doesn't have a result set.
I am not sure as to why this future never resolves. Essentially, what I am trying to do is run a coroutine in a thread and return the result in the main thread with the help of a queue/container system.