不阻塞主线程的异步 HTTP 请求
Asyncio HTTP requests without blocking main thread
我有一个主线程,它始终需要可用以读取键盘按键输入。
每次按键我都必须运行 4 个功能,例如显示、保存到文件和发出 HTTP 请求等
按键比执行 4 个功能更快。
我的逻辑如下,但看不出如何让 key_press() 始终保持 运行ning,因为我必须等待函数在某个时刻完成。
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 之后使用更多循环?或者我应该考虑多线程,让线程处于活动状态 运行ning key_press() 并在其他地方进行异步调用?
任何想法或暗示朝着好的方向发展?
如果您尝试借助 input
(您的示例中的 key_press
函数)模拟 USB 数据流,则必须使用 multithreading
模块,因为 input
是一个阻塞函数,它将停止线程中的 asyncio
循环工作。要将 asyncio
与 input
函数结合使用,您必须使用另一个线程,请查看以下示例:
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()
我有一个主线程,它始终需要可用以读取键盘按键输入。
每次按键我都必须运行 4 个功能,例如显示、保存到文件和发出 HTTP 请求等
按键比执行 4 个功能更快。
我的逻辑如下,但看不出如何让 key_press() 始终保持 运行ning,因为我必须等待函数在某个时刻完成。
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 之后使用更多循环?或者我应该考虑多线程,让线程处于活动状态 运行ning key_press() 并在其他地方进行异步调用? 任何想法或暗示朝着好的方向发展?
如果您尝试借助 input
(您的示例中的 key_press
函数)模拟 USB 数据流,则必须使用 multithreading
模块,因为 input
是一个阻塞函数,它将停止线程中的 asyncio
循环工作。要将 asyncio
与 input
函数结合使用,您必须使用另一个线程,请查看以下示例:
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()