0

In my program I want it to await user input, while continuing the loop. because in the loop an audio clip plays (a ding sound) to remind me to manually input what I cant automate on my pc.

import audio
import time
wait_for_user = input('WAITING FOR INPUT')

while(wait_for_user != 'yes'):
   wait_for_user = input('WAITING FOR INPUT')
   audio(driver,'ding.mp3')
   time.sleep(3)

Obviously it just stops at wait_for_user and doesn't continue, I just can't seem to even conceive of how to go about this.

2
  • 2
    Check out Python asyncio module (coroutines in particular). Commented Sep 15, 2021 at 21:38
  • thanks :) this is probably the way forward. Commented Sep 15, 2021 at 21:58

1 Answer 1

1

If you're on a Unix-like OS, you can use select to wait for IO:

import select

user_input = None
while True:
    time.sleep(3)
    input_ready, _, _ = select.select([sys.stdin], [], [], 0)
    for sender in input_ready:
        if sender == sys.stdin:
            user_input = input()
    if user_input is None:
      # play audio
    else:
      # user input done
      break:
Sign up to request clarification or add additional context in comments.

4 Comments

thanks alot. its probably correct, but i get this error: '[WinError 10038] An operation was attempted on something that is not a socket'
Ouch. You're on Windows! I guess this doesn't work on Windows since select is a POSIX API.
damn, have you got a windows solution?
I'm sure there's a way with asyncio as our friend mentioned in comments below your post. But for now, I have none.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.