I have an asyncio program with two tasks:
- task 1 does some work
- task 2 provides a command line interface (CLI), it reads commands from the user and sends them to task 1 for processing
The CLI is basically a loop reading lines from an asyncio stream connected to the stdin.
It works, but is not very comfortable. The problem is that the input line provides no editing commands except the [BACKSPACE ctrl-H] and [ctrl-U] which are processed at the Linux terminal level, not in the program. I want at least [LEFT] and [RIGHT] arrows and [DEL].
I tried the builtin input()
with readline
imported which gives me a comfortable editing, but it must be run in a separate thread, because it is blocking (loop.run_in_executor
).
Now the problem is the [ctrl-C] handling (KeyboardInterrupt
). The tasks are cancelled, but the input()
in its own thread still waits for the [ENTER] key. Only after an [ENTER] the application exits. This is so confusing, and it is a "blocker bug" for this approach. Unfortunately, it is not possible to kill a thread, so I am not able to make a [ctrl-C] keypress terminate the program normally.
Do you have an idea how solve the problem with input()
or do you know an alternative way how to enable basic input line editing in an asyncio task?