看python主线程是如何等待
import time
import threading
def foo(n):
for i in range(n):
print(i)
time.sleep(1)
t1 = threading.Thread(target=foo, args=(10, ), daemon=True) # 尝试替换这里的10和下面20的顺序
t1.start()
t2 = threading.Thread(target=foo, args=(20, ), daemon=False)
t2.start()
time.sleep(2)
print('Main Thread Exiting')
主线程退出,其他daemon线程的都会被杀死
import time
import threading
def bar():
while True:
time.sleep(1)
print('bar')
def foo():
print("t1's daemon = {}".format(threading.current_thread().isDaemon()))
t2 = threading.Thread(target=bar)
t2.start()
print("t2's daemon = {}".format(t2.isDaemon()))
t2.join()
t1 = threading.Thread(target=foo, daemon=True)
t1.start()
# t1.join() 如果想工作线程不能结束,取消掉这个注释
time.sleep(3)
print('Main Thread Exiting')
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。