I have a main thread which always needs to be available to read keyboard keypress input.
On each keypress I have to run 4 functions such as displaying, saving to a file, and making an HTTP request etc.
The keypresses can happen quicker than executing the 4 functions.
My logic is the following, but cannot see how I can keep key_press() always running, since I have to await the functions to finish at some point.
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())
Should I try some additional logic, with more loops after calling the asyncio.tasks recursively? Or should I look towards multi-threading, having a thread active running key_press() and the asynchronous calls elsewhere? Any ideas, or hints going towards a good direction?