-
Notifications
You must be signed in to change notification settings - Fork 39
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
ainput
won't work correctly after another ainput
call has been cancelled
#113
Comments
Hi @samskiter and thanks for the report :) I just tried the following example on linux with python 3.11 and it seems to work fine: import asyncio
import aioconsole
async def main():
try:
async with asyncio.timeout(3):
print(await aioconsole.ainput())
except TimeoutError:
print("Timeout!")
if __name__ == "__main__":
asyncio.run(main()) Could provide more information about your code, OS and python version? |
I see this bug on Windows. Try this: import asyncio
import aioconsole
async def main():
try:
async with asyncio.timeout(3):
print(await aioconsole.ainput())
except TimeoutError:
print("Timeout!")
print(await aioconsole.ainput("Double 'Enter' needed on Windows: "))
if __name__ == "__main__":
asyncio.run(main()) This is on Python 3.12.7 on Windows 10 and 11. |
I'm also having this problem. I have ainput in a loop as one of several coroutines interacting with a subprocess as part of a task group. (We're intercepting user input here so we can also insert data from elsewhere into the same queue for the server to handle.)
This mostly works, but when the subprocess ends, user_listener waits for me to hit enter before the whole task group can finish. The timeout version doesn't work for me because I don't want to interrupt the user every N seconds while the subprocess is still running. It's not a huge deal to have to hit enter to end the program, but is there any way to avoid needing to? |
For those that are looking for a working solution, the "prompt-toolkit" library has a working solution. |
ainput
won't work correctly after another ainput
call has been cancelled
@disketten |
If I understand correctly, you should be able to simply cancel the import asyncio
import aioconsole
async def user_listener():
while True:
print(await aioconsole.ainput("prompt> "))
async def do_something():
await asyncio.sleep(3)
print("\nDONE")
async def main():
async with asyncio.TaskGroup() as tg:
user_listener_task = tg.create_task(user_listener())
other_task = tg.create_task(do_something())
await other_task
user_listener_task.cancel()
if __name__ == "__main__":
asyncio.run(main()) |
You're absolutely right. I already have a limitations section in the project readme but it only addresses the need for an async-compatible python console. I'll add another section about the need for proper windows support. For some background, |
Thanks @vxgmichel! I needed to adapt that a little for my use case ( |
Similar to #104
I have a task that waits for user input but I'd like to be able to cancel that task - unfortunately is seems that I can't do that when using
ainput
- the cancellation is simply ignored and the program continues to wait for user input.Or am I missing something?
The text was updated successfully, but these errors were encountered: