In python asyncio it is straightforward if everything runs under the same event loop in one thread.
How can you pass an event from another thread that runs in normal multi-threading mode?
The closest I can find is:
In the async domain:
thing = False # global c = asyncio.Condition() # global def check_thing(): print("Checking...", thing) return thing async def hello_task(): print("acquire") await c.acquire() await c.wait_for(check_thing) c.release() print("releqse") #... def hello_notify(): # called from another thread global thing thing = True print("notify") c.notify_all()
In another thread:
hello_notify()
When hello_notify()
is called from another normal thread, it throws an RuntimeError('cannot notify on un-acquired lock')
exception.
How can this be solved without changing everything into asyncio model? I do see "acquire" printed before "notify", but "release" not printed, so the condition is "locked" I assume? Since the condition is acquired, does it mean "un-acquired" in the calling thread?
In general, how can you pass an event from another thread into an async task in python?