标签:串行 set class rom from art daemon mutex foo
主线程结束,守护线程结束。
from threading import Thread import time def sayhi(name): time.sleep(2) print("%s sat hello" % name) if __name__=="__main__": t=Thread(target=sayhi,args=(‘Ya‘,)) t.setDaemon(True) # t.daemon=True t.start() print("‘主‘线程") print(t.isAlive())
其他非守护线程结束,主线程才结束。
from threading import Thread import time def foo(): print(123) time.sleep(1) print("end123") def bar(): print(456) time.sleep(3) print("end456") if __name__ == "__main__": t1=Thread(target=foo) t2=Thread(target=bar) t1.daemon=True t1.start() t2.start() print("---main---")
多线程公用一个进程数据,所以无需像多进程那样穿入mutex对象
from threading import Thread,Lock n=100 import time def task(): global n mutex.acquire() temp=n time.sleep(0.1) n=temp-1 mutex.release() if __name__=="__main__": t_l=[] mutex=Lock() for i in range(100): t=Thread(target=task) t_l.append(t) t.start() for t in t_l: t.join() print("主线程:",n)
标签:串行 set class rom from art daemon mutex foo
原文地址:https://www.cnblogs.com/yaya625202/p/9039043.html