#π Stop input() from catching inputs during sleep()
37 messages Β· Page 1 of 1 (latest)
@rain cape
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
I have no idea if this would work, but you could try wrapping the call to sleep in a STDOUT redirect with wrapper, and temporarily redirect the text into the void.
Sorry, STDIN.
How would that be written out? Im not very familiar with wrappers
I was thinking of this, but that's only for STDOUT.
unfortunate
Sec
from io import BytesIO
from time import sleep
class SilenceInput:
def __init__(self):
self.old_stdin = None
def __enter__(self):
self.old_stdin = sys.stdin
sys.stdin = BytesIO()
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdin = self.old_stdin
print("Prepare for prompt")
with SilenceInput():
sleep(5)
fav_food = input("What is your favorite food")
print(fav_food)
This may be a tad hacky, but I'm not sure how else to handle this. Basically, it temporarily overwrites sys.stdin to a bytes IO object, then restores it when the with block exits.
Seems to work for what you need. And you're welcome. I typed that pretty fast though and only tested it once, so you'll want to do some testing of your own and learn about context managers if you haven't already.
Def will look into them
istr MS-DOS of all things had a program to flush the input buffer, for just this sort of situation
Ah, ya, flushing the stream in __exit__ instead of reassinging sys.stdin would probably work too and be less hacky.
It does.
import sys
from time import sleep
class SilenceInput:
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdin.flush()
print("Prepare for prompt")
with SilenceInput():
sleep(5)
# If the user presses ENTER here, the following input() will catch it later.
fav_food = input("What is your favorite food")
print(fav_food)
Although at this point, the purpose of the context manager is kind of moot. Might as well just do the flush after sleep inline.
Wait, nvm, my testing was flawed. The IDE was masking the issue π€¦ββοΈ
Sorry, my way doesn't actually work. Sorry if it lead you down thw wrong path.
Ya, context managers (which "wrap" code) are generally very useful.
This doesn't seem to be working also
Ya, I'm not sure why they don't work. I haven't really ever needed to do anything like this before, so it was more experimentation.
You can poll stdout to see how much can be read, then read and ignore that, immediately after the sleep
Although, if enter hasn't been pressed, it's still only your terminal that's in control
Use curses if you really, really don't want the user to input anything beforehand
or you could do something silly, but probably working, by just timing the time it took to enter something... if it's to fast, just re-prompt the user:
from time import sleep, time
print("Prepare for prompt")
sleep(2)
while True:
start = time()
fav_food = input("What is your favorite food?: ")
if time() - start > 0.1:
break
print()
print('your fav food:', fav_food)
CPU fans go brrrrrrr
not really. no.
it only repeats as often as you manage to hit enter in those two seconds, and then it waits normally
Ah, right, nvm.
though you will see the prompt multiple times, but I'd say that's a reasonable compromise.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.