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
async input() #9144
Comments
I tried using async def ainput():
while(True):
x = sys.stdin.read(100)
if(len(x) > 0):
return x
await asyncio.sleep(0.1) |
you can use Right now this is a boolean. In 9.1.0 it will change to an int telling you how many characters are available. See #9141 You can also use |
There are some impressively complicated ways to do this in CPython (desktop Python): https://stackoverflow.com/questions/35223896/listen-to-keypress-with-asyncio |
Interesting. So if I only have single-byte characters, I can use the count from usb_cdc like so: async def ainput():
usb_cdc.console.timeout = 0
x = ""
while(True):
n = usb_cdc.console.in_waiting
x += sys.stdin.read(n)
if('\n' in x):
return x
await asyncio.sleep(0.1) But then it breaks if I press a "delete" character. Also interesting is that the input isn't echoed back this way. |
One of the many uses of async coroutines is waiting for user input while completing background tasks.
There are examples for buttons, but digitalIO.value wasn't a blocking function anyway so polling it is trivial.
My issue is that I am trying to use the serial console to interact with the device, and
input()
is most definitely a blocking function.The implementations I've found seem to be platform-dependent, communicating with a separate UI thread or relying on non-standard libraries. Even the aioconsole library seems to do this.
It seems there should be a non-blocking way to read from sys.stdio, as it's a stream. There is some discussion of this in the circuitpython docs, but no methods given. The asyncio streams appear to be TCP-only.
The text was updated successfully, but these errors were encountered: