Python event loop does not work properly with stdin(使用标准输入时,Python事件循环无法正常工作)
本文介绍了使用标准输入时,Python事件循环无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在试着理解Python的异步操作。我写这段代码只是为了演示清楚概念。
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))
我从单独的线程运行事件循环,并尝试在运行时创建循环中的任务。但是我不明白为什么这个代码不起作用。它接受输入,但随后转到下一迭代,而不打印printer
函数中的任何内容。
令人惊讶的是,如果我不从stdin
接受输入,而只是使用这样的硬编码消息
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
一切都运行得很好。输出
[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
请使用input
推荐答案
您在第一次设置中使用异步不正确。您应该不需要将其与线程模块一起插入。
我推荐的设置是创建一个异步函数main,它包含一个无限循环,您可以在其中请求输入和创建任务。然后,您可以在声明完事件循环之后从事件循环中运行Main。 请注意,在上面的设置中,等待您在Main内部创建的任务是可选的;因为保证stdout由内核同步(我有70%的把握这是真的),您可以一次有任意多的任务运行Print()。但是,如果您确实在等待任务,则在用户尝试输入时,您的程序将不会打印输出;它将调用Printer(),该函数首先写入标准输出,并且仅在Printer()完成后才请求下一组输入。希望这回答了您的问题。请参阅下面的文档作为附加资源。
https://docs.python.org/3/library/asyncio-task.html
这篇关于使用标准输入时,Python事件循环无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用标准输入时,Python事件循环无法正常工作


猜你喜欢
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 沿轴计算直方图 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01