[英]Asyncio HTTP requests without blocking main thread
我有一个主线程,它总是需要可用于读取键盘按键输入。
在每次按键时,我必须运行 4 个功能,例如显示、保存到文件和发出 HTTP 请求等。
按键可以比执行 4 个功能更快。
我的逻辑如下,但看不出如何让 key_press() 始终运行,因为我必须等待函数在某个时刻完成。
async def main():
async with aiohttp.ClientSession() as client:
while True:
pressed_key = key_press()
if pressed_key != "":
f1 = asyncio.create_task(do_something1())
f2 = asyncio.create_task(do_something2())
f3 = asyncio.create_task(do_something3())
f4 = asyncio.create_task(send_to_API())
await f1,f2,f3,send_to_API
asyncio.run(main())
我是否应该尝试一些额外的逻辑,在递归调用 asyncio.tasks 之后使用更多循环? 或者我应该考虑多线程,让一个线程处于活动状态运行 key_press() 并在其他地方进行异步调用? 任何想法或暗示朝着好的方向发展?
如果您尝试在input
的帮助下模拟 USB 数据 stream(在您的示例中为key_press
function),则必须使用multithreading
模块,因为input
是阻塞asyncio
并且它将停止线程中的异步循环工作。 要将asyncio
与input
function 结合使用,您必须使用另一个线程,请查看以下示例:
import asyncio
import threading
async def do_work(i):
"""take input key and do some work with the input"""
await asyncio.sleep(5)
print(i)
async def main_thread_loop_work(_key):
"""simulate multiple tasks caused by key input"""
t1 = asyncio.create_task(do_work(_key))
t2 = asyncio.create_task(do_work(_key))
t3 = asyncio.create_task(do_work(_key))
await t1
await t2
await t3
def thead_worker(_key):
"""target function for another thread"""
asyncio.run(main_thread_loop_work(_key))
if __name__ == '__main__':
while True:
some_key = input("Please provide any key: ")
th = threading.Thread(target=thead_worker, args=(some_key, ))
th.start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.