0

I'd like to do some actions while waiting for a user input: I was thinking of:

var = raw_input("what are you thinking about")
while var == None:
   dosomethingwhilewaiting()
print "input is:", var

but raw_input is blocking until a user input come in. Any ideas?

martineau
  • 119,623
  • 25
  • 170
  • 301
user1473508
  • 195
  • 1
  • 5
  • 15
  • 1
    The program is not going to execute untill someone provide any kind of data to the "var" variable. It will just wait for user to press enter, so code like this one, will never work as you want it to work. – Nf4r Oct 02 '16 at 19:41
  • This is not a duplicate of this question. Like at least try to read the question, before you mark it like that. – Nf4r Oct 02 '16 at 19:43
  • 1
    Something like [this](http://stackoverflow.com/questions/32286049/python-accept-input-while-waiting) or [this](http://stackoverflow.com/questions/20576960/python-infinite-while-loop-break-on-user-input) should work. Essentially you need to break up the input task into a separate thread from the "dosomething" task. – Cory Kramer Oct 02 '16 at 19:45
  • so this is a duplicate of something then? – Jean-François Fabre Oct 02 '16 at 19:49

1 Answers1

1

you can use threads.

import thread
import time

var = None
def get_input():
    global var
    var = raw_input("what are you thinking about")

thread.start_new_thread(get_input, ())

i = 0
while var == None:
    i += 0.1
    time.sleep(0.1)
print "input is:", var
print "it took you %d seconds to answer" % i
Nima Ghotbi
  • 641
  • 3
  • 9