time.sleep()时的交互?

时间:2018-12-16 15:39:05

标签: python arrays time

假设我想让程序使用

等待10秒
time.sleep(10)

这10秒钟的睡眠期间是否有机会与程序进行交互?

1 个答案:

答案 0 :(得分:1)

我不清楚您的意思,但这是一个简单的程序,允许用户在睡眠时输入,然后在最后调用一个函数:

import threading, time

def foo():
    #function that accepts the input
    print('You typed:', input('Type something'))

def bar():
    #sleeping thread function
    time.sleep(10)
    print('Done!')

thread1 = threading.Thread(target=foo)  #thread for foo
thread2 = threading.Thread(target=bar)  #thread for bar
thread1.start()   #run foo thread
thread2.start()   #run bar thread

请注意,由于python在等待input()时无法输出任何内容,因此即使输入了10秒的计时器,它也不会打印Done!,直到给出输入为止。