问题
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
回答1:
You’re using asyncio improperly in the first setup. You shouldn’t need to interpose it with the threading module.
My recommended setup for this is to create an async function main which contains an infinite loop where you ask for input and create tasks. Then, you can run main from an event loop after you’re done declaring it.
Mind you, awaiting the tasks you create inside main in the above setup is optional; since stdout is guaranteed to be synchronized by the kernel (I’m 70%ish sure this is true,) you can have as many tasks as you want running printer() at a time. However, if you do await the tasks, your program won’t print out while the user is trying to input; it’ll call printer(), which writes to stdout, first, and only ask for the next set of input after printer() has completed.
Hope that answers your question. See the below documentation as an additional resource.
https://docs.python.org/3/library/asyncio-task.html
来源:https://stackoverflow.com/questions/56350166/python-event-loop-does-not-work-properly-with-stdin