根据时间和键盘中断从另一个python脚本运行python脚本

时间:2018-02-15 17:52:01

标签: python

我创建了两个python脚本:一个用户输入,另一个用无限循环。我想运行第一个脚本,如果用户在2秒内没有按任何键,它会自动进入第二个脚本,当它进入第二个脚本时,如果按下某个键,用户仍然可以中断它。 这继续和屏幕保护机制一样。

我能够使超时部分正确,但它不会回到第一个脚本。请帮忙

以下是我的两个脚本:

Script 1:

 import subprocess
    from threading import Timer
    out_of_time=False
    def time_ran_out():
        subprocess.call("python s2.py 1", shell=True)
        out_of_time=True
    t=Timer(2,time_ran_out)
    t.start()
    while 1==1:
        x = input("Enter here: ")
        if x!=None and not out_of_time:
            print(x)
    t.cancel()

脚本2:

try:
    while True:
        print ("Hello World")
except KeyboardInterrupt:
    pass

所以输出应该是

Enter here:

如果2秒内没有按任何键,它应该开始运行第二个脚本:

Hello World
Hello World

如果按下某个键,它应该再次返回第一个脚本。

请帮忙

谢谢

0 个答案:

没有答案