[英]Terminate Python program if input isn't specified in due timeframe
I'm trying to terminate python program if an input isn't provided by user within specific time frame, say - 5 seconds.如果用户在特定时间范围内未提供输入,例如 - 5 秒,我将尝试终止 python 程序。
The code has been taken and edited from mediocrity's answer该代码已从平庸的答案中提取和编辑
import sys
import time
from threading import Thread
x = None
def check():
global x
time.sleep(5)
if x:
print("Input has been given!")
return
sys.exit()
Thread(target=check).start()
x = input("Input something: ")
But it keeps waiting for the input and doesn't terminate, unless the input is given.但是它一直在等待输入并且不会终止,除非给出了输入。 How could I change the code so that it executes as inteded?
我怎样才能更改代码以使其按原样执行?
you should try this code its working on vscode你应该试试这个代码它在 vscode 上的工作
import sys
import time
from threading import Thread
x = None
def check():
global x
time.sleep(5)
if x:
print("Input has been given!")
return
else:
print("input has not been given for last 5 sec")
sys.exit()
if __name__=='__main__':
t1=Thread(target=check)
t1.start()
x = input("Input something: ")
t1.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.