python 退出子线程_如何在Python中退出主线程并使子线程在后台继续

我的目标是让子线程在后台运行,而主线程应该先启动它们然后退出:

我尝试了以下代码: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()不是答案,因为主线程会阻塞。在

我根本不想主线程阻塞。在

这个问题有解决办法吗?在

提前谢谢。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值