I am trying to get my head around python's asyncio. I have written this piece of code just for demonstration to clear concept.
import asyncio
import threading
async def printer(b, a):
print(b)
await asyncio.sleep(5)
print(a)
def loop_runner(loop):
print('[RUNNING LOOP]')
loop.run_forever()
if __name__ == '__main__':
event_loop = asyncio.get_event_loop()
# run_forever() is blocking. running it from separate thread
loop_thread = threading.Thread(target=loop_runner, args=(event_loop,))
loop_thread.start()
while True:
before, after = input('Before :'), input('After :')
event_loop.create_task(printer(before, after))
I am running event loop from separate thread and trying to create tasks in loop on runtime. But I can't understand why this code is not working. It takes input but then goes to next iteration without printing anything from printer function.
Surprisingly, if I do not take inputs from stdin, and just use hard coded messages like this
messages = [('Hello', 'world'), ('Foo', 'bar'), ('Alice', 'Bob')]
for message in messages:
before, after = message
coroutine = printer(f'[ITERATION] {count} [MESSAGE] {before}', f'[ITERATION] {count} [MESSAGE] {after}')
event_loop.create_task(coroutine)
count += 1
everything just works fine. Output
[RUNNING LOOP]
[ITERATION] 0 [MESSAGE] Hello
[ITERATION] 1 [MESSAGE] Foo
[ITERATION] 2 [MESSAGE] Alice
[ITERATION] 0 [MESSAGE] world
[ITERATION] 1 [MESSAGE] bar
[ITERATION] 2 [MESSAGE] Bob
Please help me understand this behavior with input