python time.sleep对进程、线程生命周期的不同影响
time.sleep()方法用于在特定时间内停止程序的执行。
进程在执行时,time.sleep()不会对其有影响
线程在执行时,time.sleep()会对线程有限制作用
import time
import multiprocessing
def sing(num,name):
for i in range(num):
print(name + ' sing')
time.sleep(2)
if __name__ == '__main__':
sing_process = multiprocessing.Process(target=sing,args=(5,'lingling'))
sing_process.start()
time.sleep(2)
import time
import threading
def work():
for i in range(20):
print(i)
print("work")
time.sleep(0.2)
if __name__ == "__main__":
work_thread = threading.Thread(target=work)
work_thread.daemon=True
work_thread.start()
time.sleep(4)
原因:time.sleep()方法会阻止线程,对整体的时间有限制。
有问题可以留言讨论一下呢,初学没啥基础