Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to keypress with asyncio

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

like image 605
kharandziuk Avatar asked Feb 05 '16 12:02

kharandziuk


2 Answers

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:

import sys
import functools
import asyncio as aio

class Prompt:
    def __init__(self, loop=None):
        self.loop = loop or aio.get_event_loop()
        self.q = aio.Queue()
        self.loop.add_reader(sys.stdin, self.got_input)

    def got_input(self):
        aio.ensure_future(self.q.put(sys.stdin.readline()), loop=self.loop)

    async def __call__(self, msg, end='\n', flush=False):
        print(msg, end=end, flush=flush)
        return (await self.q.get()).rstrip('\n')

prompt = Prompt()
raw_input = functools.partial(prompt, end='', flush=True)

async def main():
    # wait for user to press enter
    await prompt("press enter to continue")

    # simulate raw_input
    print(await raw_input('enter something:'))

loop = aio.get_event_loop()
loop.run_until_complete(main())
loop.close()

EDIT: I removed the loop parameter form Queue as it is removed in 3.10.

Also, these days I use structured concurrency (trio), and if anyone is curious this is pretty easy to do in trio:

import trio, sys
  
async def main():
    async with trio.lowlevel.FdStream(sys.stdin.fileno()) as stdin:
            async for line in stdin:
                if line.startswith(b'q'):
                    break
                print(line)


trio.run(main)
like image 144
bj0 Avatar answered Oct 13 '22 19:10

bj0


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 to stdin and stdout.

Here's an example:

import asyncio
import aioconsole

async def echo():
    stdin, stdout = await aioconsole.get_standard_streams()
    async for line in stdin:
        stdout.write(line)

loop = asyncio.get_event_loop()
loop.run_until_complete(echo())

It also includes an asynchronous equivalent to input:

something = await aioconsole.ainput('Entrer something: ') 

It should work for both file and non-file streams. See the implementation here.

like image 45
Vincent Avatar answered Oct 13 '22 19:10

Vincent