raw_input为空时执行操作 [英] do action while raw_input is empty

查看:76
本文介绍了raw_input为空时执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在等待用户输入时执行一些操作: 我在想:

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

,但是raw_input在用户输入进入之前一直处于阻塞状态. 有什么想法吗?

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

推荐答案

您可以使用线程.

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

这篇关于raw_input为空时执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆