Can somebody provide a sample of code which listen to keypress in nonblocking manner with asynio and put the keycode in console on every click?
It's not a question about some graphical toolkit
Can somebody provide a sample of code which listen to keypress in nonblocking manner with asynio and put the keycode in console on every click?
It's not a question about some graphical toolkit
So the link provided by Andrea Corbellini is a clever and thorough solution to the problem, but also quite complicated. If all you want to do is prompt your user to enter some input (or simulate raw_input), I prefer to use the much simpler solution:
I wrote something similar as part of a package called aioconsole.
It provides a coroutine called
get_standard_streams
that returns two asyncio streams corresponding tostdin
andstdout
.Here's an example:
It also includes an asynchronous equivalent to
input
:It should work for both file and non-file streams. See the implementation here.
An alternative to using queues would be to make the command line an asyn generator, and process the commands as they come in, like so: