I want to finish a python script(func) which has below functions:
1.there are two threads,one thread print a updating line which shows the left time before input to the terminal
2.another thread waiting for my input to the terminal(sys.stdin)
3.my question is,while i use my func like:
print '''input your file path,the default value will be "/root/targets.txt":>'''
get_input_intime([''],'/root/targets.txt',20)
but I can not input a long file abs path,coz the first thread is always print the left time,and I want to achieve:
a)there are two lines in the terminal
b)the first line as the first thread's print string's place
c)the second line will accept my input,so i can input a file path in my need and will not be flushed by the first thread,like this:
in terminal:
first line:sys.stdout.write("%s seconds left...please input your chioce:>\r" % timeout)
second line:(wainting for my input)
4.my code is:
1)get chioce in some seconds,if no input,return the default chioce
2)the first param is a list contain undefault chioces.
eg.there are four chioce 1,2,3,4,if I want to make the default chioce as 2,then the undefault_chioce_list is ['1','3','4'],each of the list is str type.
3)the second param is the default chioce value
4)the third param is the time before input(choose)
5)the return value is the chioce from raw_input()
class MyThread(threading.Thread):
def __init__(self,func,args,name=''):
threading.Thread.__init__(self)
self.name=name
self.func=func
self.args=args
def run(self):
self.result=apply(self.func,self.args)
def get_result():
return self.result
def get_input_intime(undefault_chioce_list,default_choose,timeout=5):
default_choose=[default_choose]
timeout=[timeout]
choosed=[0]
chioce=['']
def print_time_func():
while (choosed[0]==0 and timeout[0]>0):
sys.stdout.write("%s seconds left...please input your chioce:>\r" % timeout)
sys.stdout.flush()
time.sleep(1)
timeout[0]-=1
if choosed[0]==0:
chioce[0]=default_choose[0]
def input_func():
rlist, _, _ = select([sys.stdin], [], [], timeout[0])
if rlist:
chioce_and_enter = sys.stdin.readline()
choosed[0]=1
if chioce_and_enter[0] not in undefault_chioce_list:
chioce[0]=default_choose[0]
else:
chioce[0]=chioce_and_enter[0]
print "you choosed %s" % chioce[0]
else:
print "\nyou didn't input..."
print "I will choose the default chioce for you:%s" % default_choose[0]
time_left_thread=MyThread(print_time_func,())
input_thread=MyThread(input_func,())
time_left_thread.start()
input_thread.start()
time_left_thread.join()
input_thread.join()
return chioce[0]