Python · 2012/07/08

简单搞定 Input blocking 问题

目的: Python调用raw_input时,需要等待用户输入。如果用户不输入,这个程序就block了。可以在raw_input前开启一个定时器,如果用户有输入则停止定时器;如果用户没有输入,等待定时器超时后自动发送一个按键模拟用户输入。

需要资源:老外基于python开发的,最高只支持python2.6。到这里下载安装 http://www.rutherfurd.net/python/sendkeys/

代码

   1: import SendKeys

   2: import threading

   3:  

   4: def noInput():

   5:     SendKeys.SendKeys("""     {ENTER}    """)

   6:     print " User did not make choice, automatically Send "ENTER" "

   7:  

   8: waitInput = threading.Timer(30, noInput)

   9: waitInput.start()   

  10: choice = raw_input()

  11: waitInput.cancel()