do action while raw_input is empty

2019-09-04 06:05发布

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?

标签: python input
1条回答
叼着烟拽天下
2楼-- · 2019-09-04 06:51

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
查看更多
登录 后发表回答