Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio support #154

Open
jquast opened this issue Mar 12, 2020 · 7 comments
Open

asyncio support #154

jquast opened this issue Mar 12, 2020 · 7 comments

Comments

@jquast
Copy link
Owner

jquast commented Mar 12, 2020

Maybe we can be of help, here.

Propose:

  • asyncio=True in the class constructor, swaps out any inkey() and similar methods with await_inkey() and other asyncio definitions at instantiation time, when initialized Terminal(asyncio=True).
  • This would let inkey() and others function as documented without asyncio, or, with asyncio, to function with the same behavior and signature, but to use expression await Terminal.inkey() with asyncio=True argument to class constructor.
@haliphax
Copy link
Collaborator

This would be awesome! My shim is good enough for what I'm building, but I would love to have asyncio supported further upstream in blessed itself. Cheers.

@wagnerflo
Copy link

Since I could use asyncio support in blessed for a future project of mine, I'm willing to tackle this, provided there is still interest in including this in the library and provided I get a bit of input and help on the design decisions that follow.

@avylove
Copy link
Collaborator

avylove commented May 14, 2021

I think any help would be appreciated, though I can't guarantee how quickly we'll be able to respond to PRs.

@da-h
Copy link

da-h commented Jun 19, 2021

Hey,

I've got an easy workaround for an asyncio-compatible inkey.
The idea is to run inkey in a seperate Thread and pass the result to a asyncio.Queue.

The example script waits for a user-input in a asyncio-loop.
To show that it is non-blocking, the script timeouts the query with a message.

import asyncio
import threading
from asyncio import Queue
from blessed import Terminal

queue = Queue()
term = Terminal()

def threaded_inkey():
    running = True
    with term.cbreak():
        while running:
            key = term.inkey()
            if key == "q":
                running = False
            queue.put_nowait(key)
            queue._loop._write_to_self()

async def mainloop():
    running = True
    with term.fullscreen():
        print("press a key. (exit using 'q')")
        while running:
            try:
                key = await asyncio.wait_for(queue.get(), 1)
                if key == "q":
                    running = False
                print("key pressed:", key)
            except asyncio.TimeoutError:
                print("no key pressed")

loop = asyncio.get_event_loop()
threading.Thread(target=threaded_inkey).start()
loop.run_until_complete(mainloop())

Do you want me to add a PR to add this to the documentation?

Best,
da-h

@haolian9
Copy link

haolian9 commented Jun 24, 2021

as asyncio has been considered, is there enough room for trio?

--- update

maybe an useful reference: starlette has supported trio via anyio

--- update
i created a gist that implements inkey in trio

@wagnerflo
Copy link

Since I've recently started playing around with trio and I like it a bit better even than asyncio, my current plan was to have a look if this is easily doable using anyio. It's on my ToDo list and since this is relevant for a project at work I'll get to it at one point for sure, but currently can't promise a timeline.

@jquast
Copy link
Owner Author

jquast commented Jul 21, 2021

Thank you @wagnerflo, @da-h, and @haolian9 for your initiative to add asyncio support to blessed. I'm glad to see there is some interest here. I forgot to link @haliphax's inkey() implementation, which I think is very nice, please review https://github.com/haliphax/xthulu/blob/master/xthulu/terminal.py#L170-L227

Just a reminder that for this change to be accepted it will need,

  • Documentation & Functional example
  • Automatic tests & full coverage
  • Windows support & testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants