Reputation: 615
In Python, in what way can I do something on loop while user has not given an input. I have python client and I ask for user input, but while user has not given an input I'd like to keep giving updates every five second.
I can't use
while (raw_input==""):
update()
since if input is "", it means user already entered something (hit enter);
Is there a way to do it?
UPDATE:
I still can't get anything to work for me. I tried the method below, also tried something similar to this thread: waiting for user input in separate thread. And I also tried instead passing an update() method to the thread that's supposed to be working in the background. But anything with raw_input() makes it stuck on waiting for input:
import threading
import time
def update():
while True:
time.sleep(5)
print "update"+'\n'
mythread = threading.Thread(target=update, args=())
mythread.daemon = True
mythread.start()
while True:
usrin=raw_input()
print "you typed: "+usrin
Every time user inputs something, update is done and then usrin is given back. If this was what I wanted I could just easily put the update() into the last while loop.
If I were to just start this thread with update method and do nothing else in the program (scrap the last while loop), then in shell while it updates every five seconds I can still use the shell (i.e type 3+5 and it gives me 8). I want something like that to happen within the program, when user is inactive update, and if he types react and go back to updating.
NOTE: Also, I'm currently using python 2.7.8, would switching versions help?
Upvotes: 1
Views: 2117
Reputation: 2120
Maybe this can get you started:
from threading import Thread
from time import sleep
result = None
def update_every_second():
while result is None:
sleep(1)
print "update"
t = Thread(target=update_every_second)
t.start()
result = raw_input('? ')
print "The user typed", result
Upvotes: 1