马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#coding=utf-8
'''
@ BY Cool_Breeze
@ 2020/08/06
@ 多线程接受用户输入
@ Rev 01
'''
from threading import Thread
from queue import Queue
import time
import os, sys
class input_wiat:
'''
@ string 提示字符串
@ sec 等待时间 time.slee()
@ fileno IO文件描述
'''
def __init__(self, string, sec, fileno):
self.string = string
self.sec = sec
self.fileno = fileno
self.q = Queue()
self.qb = Queue()
def start(self):
def f(q):
while True:
try:
sys.stdin = os.fdopen(self.fileno)
n = int(input(self.string))
q.put(n)
break
except ValueError:
continue
p = Thread(target=f, args=(self.q,))
p.setDaemon(True)
p.start()
def wait(self):
def f(qb):
while True:
time.sleep(self.sec)
qb.put(True)
p = Thread(target=f, args=(self.qb,))
p.setDaemon(True)
p.start()
def result(self):
self.wait()
self.start()
while True:
if not self.q.empty():
return self.q.get()
if not self.qb.empty():
return 0 #deflaut
if __name__ == '__main__':
t = input_wiat('请输入:\n',5 , sys.stdin.fileno())
print('返回结果 :{}'.format(t.result()))
|