我的目标是让子线程在后台运行,而主线程应该先启动它们然后退出:
我尝试了以下代码:import time
import logging
import threading
logging.basicConfig(filename="threading.log",level=logging.DEBUG)
def worker(count):
for c in range(count,-1,-1):
threadname = threading.currentThread().getName()
threadcount = threading.active_count()
threadnames = threading.enumerate()
logging.debug("Child thread: {} continuing with threadcount {} {} and counter value: {}".format(threadname,threadcount,threadnames,c))
time.sleep(2)
mainthread = threading.currentThread().getName()
print ("Starting main thread:",mainthread)
t1 = threading.Thread(target=worker,args=(10,))
t1.setDaemon(True)
t1.start()
time.sleep(5)
print ("Attempting to close main thread:",mainthread)
但是一旦主线程退出,我认为子线程也会退出,因为我在线程.log(我用子线程创建的)
^{pr2}$
我知道使用join()不是答案,因为主线程会阻塞。在
我根本不想主线程阻塞。在
这个问题有解决办法吗?在
提前谢谢。在