python threading setdaemon_threading.Thread的setDaemon()方法

下面是一些使用线程的基本代码:import Queue

import threading

def basic_worker(queue):

while True:

item = queue.get()

# do_work(item)

print(item)

queue.task_done()

def basic():

# http://docs.python.org/library/queue.html

queue = Queue.Queue()

for i in range(3):

t = threading.Thread(target=basic_worker,args=(queue,))

t.daemon = True

t.start()

for item in range(4):

queue.put(item)

queue.join() # block until all tasks are done

print('got here')

basic()

当你运行它,你得到% test.py

0

1

2

3

got here

现在评论一下:t.daemon = True

再次运行它,您将看到脚本打印相同的结果,但挂起。

主线程结束(注意got here已打印),但第二个线程从未结束。

相反,当t.daemon设置为True时,线程t在主线程结束时终止。

注意,“守护进程线程”与daemon processes没有什么关系。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值