Closed prprpr777 closed 3 years ago
Client 输入部分子线程已经完成,代码如下:
def getInput(prompt: str, inputType: type = str, timeout: float = 0) -> Any:
flag: bool = True
temp: str
print("Action timeout: %fs" % (timeout, ))
while flag:
try:
temp = input(prompt)
except EOFError:
return KeyboardInterrupt()
if inputType != str:
try:
return eval(temp)
except:
print("Input type mismatch!")
else:
return temp
class ReadInput(Thread):
"""
The input thread, will be interrupted by KeyBoardInterruption
"""
def __init__(self, prompt: str, inputType: type = str, timeout: float = 0):
super().__init__()
self.inputType = inputType
self.timeout = timeout
self.result: Any = None
self.prompt = prompt
def run(self) -> Any:
if self.timeout == 0:
self.result = getInput(self.prompt, self.inputType, self.timeout)
else:
dest: ReadInput = ReadInput(self.prompt, self.inputType)
dest.setDaemon(True)
dest.start()
dest.join(self.timeout)
self.result = dest.getResult()
if self.result is None:
print("Input timeout.")
def getResult(self) -> Any:
"""
Get the return value of the input
- inputType: if the input is correctly processed
- None: if timeout
- KeyboardInterrupt: if Ctrl-C is pressed
"""
return self.result
inputType
参数对应类型的变量None
KeyboardInterruption