threading:

    t.setDaemon(True)  将线程设置成守护线程,主进行结束后,此线程也会被强制结束。如果线程没有设置此值,则主线程执行完毕后还会等待此线程执行。

    t.join() 线程阻塞,只有当线程运行结束后才会继续执行后续语句

示例:

#coding: utf-8
import threading
import time def foo(name):
time.sleep(2)
print 'this is %s \n' % (name,) if __name__ == '__main__':
mythread = []
for i in range(5):
t = threading.Thread(target=foo, args=(i, ))
# t.setDaemon(True)
t.start()
mythread.append(t)
# for t in mythread:
# t.join()
print '-- end --'

运行结果(注意,print为非线程安全,所以打印内容有时会比较乱):

-- end --

this is 1

this is 2

this is 4

this is 0

this is 3

Process finished with exit code 0

可以看到,主线程和子线程是独立运行的(最后一行先被打印),主线程运行结束后依旧等待子线程结束。

把上面的t.setDaemon(True)取消注释,再运行一遍,发现只打印了如下内容:

-- end --

Process finished with exit code 0

即主线程运行结束后,会强制结束掉子线程

我们继续再把下面的注释去掉

# for t in mythread:

#     t.join()

再运行一遍,输出如下:

this is 1

this is 0

this is 3

this is 4

this is 2

-- end --

Process finished with exit code 0

注意:end在最后才被输出,说明在join那里阻塞了主线程的运行,在等待子线程运行完成。

queue:

       q.put(item) 将item放入队列中。

       q.get() 从队列中取出数据。

       q.task_done() 每次从queue中get一个数据之后,当处理好相关问题,最后调用该方法,以提示q.join()是否停止阻塞,让线程向前执行或者退出;

       q.join() 阻塞,直到queue中的数据已经每项已经task_done处理到空。

       如果不使用task_done也可以,可以通过q.full() q.empty()等来判断

 

 

q.join()隐藏的问题:

       对于生产者-消费者模型,这种阻塞方式是有漏洞的,因为如果queue初始为空,q.join()会直接停止阻塞,继续执行后续语句。

       还有另一种情况,就是生产者生产速度比较慢,而消费者消费速度比较快,也有可能停止阻塞,继续执行后续语句

       如果有多个消费者,没有生产者,且queue始初化为一定的数据量,则可以正常执行。

示例:

#coding: utf-8
import Queue
import threading
import time queue = Queue.Queue(maxsize=3) def produce():
for i in range(5):
item = "item" + str(i)
queue.put(item)
print "%s produce" % (item, )
# time.sleep(4) def customer():
while True:
time.sleep(2)
item = queue.get()
print "process %s finished" % (item, )
queue.task_done() if __name__ == '__main__':
t = threading.Thread(target=produce)
t.setDaemon(True)
t.start()
for i in range(3):
c = threading.Thread(target=customer)
c.setDaemon(True)
c.start()
queue.join()
print '-- end --'

运行输出如下:

item0 produce

item1 produce

item2 produce

process item0 finished

process item2 finishedprocess item1 finished

item3 produce

item4 produce

process item3 finished

process item4 finished

-- end --

Process finished with exit code 0

可以看到程序正常结束,end字符也在最后在打印出来。

我们把produce函数中注释掉如下语句,

queue.put(item)

print "%s produce" % (item, )

运行得到:

-- end --

Process finished with exit code 0

即queue为空,join()方法并不阻塞线程。

我们在produce函数中加上sleep,让生产慢一点,去掉time.sleep(4)前面的注释,运行得到:

item0 produce

process item0 finished

-- end --

Process finished with exit code 0

有时运行有可能会得到这样的错误信息:

Exception in thread Thread-3 (most likely raised during interpreter shutdown)

报错信息表明主线程不等待子线程就结束了。

可以发现,其实produce函数中应该还有任务要生成,但因为太慢,在join语句那里检测到队列为已经全部被设置task_done,就会继续往后执行,这有可能有时并不我们需要的,针对这样的情况,我们可以在queue.join()前加多一个子句t.join()即可达成目的。

如果我们再把customer函数中的queue.task_done()函数去掉,运行得到

item0 produce

process item0 finished

item1 produce

process item1 finished

item2 produce

process item2 finished

item3 produce

process item3 finished

item4 produce

process item4 finished

注意程序一直没有结束,而最后一行end语句也没有出现。因为在join那里,虽然队列已经全部为消费完,已经为0,但是由于它不是调用task_done函数而让其计数为0,所以此时,函数会一直阻塞在这里。

python threading queue模块中join setDaemon及task_done的使用方法及示例的更多相关文章

  1. 后台程序处理(二) python threading - queue 模块使用

    由于协程没办法完成(一)中所说的任务模式 接下来就尝试一下使用线程和队列来实现一下这个功能 在实现之前,我们先明确一个问题--python的线程是伪并发的.同一时间只能有一个线程在运行.具体怎样的运作 ...

  2. [转]Python多线程与多线程中join()的用法

    https://www.cnblogs.com/cnkai/p/7504980.html Python多线程与多进程中join()方法的效果是相同的. 下面仅以多线程为例: 首先需要明确几个概念: 知 ...

  3. python基础:os模块中关于文件/目录常用的函数使用方法

    Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...

  4. Python的collections模块中namedtuple结构使用示例

      namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...

  5. Python使用functools模块中的partial函数生成偏函数

    所谓偏函数即是规定了固定参数的函数,在函数式编程中我们经常可以用到,这里我们就来看一下Python使用functools模块中的partial函数生成偏函数的方法 python 中提供一种用于对函数固 ...

  6. 【转】在Python的struct模块中进行数据格式转换的方法

    这篇文章主要介绍了在Python的struct模块中进行数据格式转换的方法,文中还给出了C语言和Python语言的数据类型比较,需要的朋友可以参考下 Python是一门非常简洁的语言,对于数据类型的表 ...

  7. Python的Django框架中forms表单类的使用方法详解

    用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...

  8. Python之Queue模块

    Queue 1.创建一个“队列”对象 >>> import Queue >>> queue = Queue.Queue(maxsize=100) >>& ...

  9. Python队列queue模块

    Python中queue模块常用来处理队列相关问题 队列常用于生产者消费者模型,主要功能为提高效率和程序解耦 1. queue模块的基本使用和相关说明 # -*- coding:utf-8 -*- # ...

随机推荐

  1. HDU2825 Wireless Password

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

  2. [51nod1213]二维曼哈顿距离最小生成树

    二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间的距离为:横纵坐标的差的绝对值之和,即:Abs(x1 - x2) + Abs(y1 - y2)(也称曼哈顿距离).求这N个点所组成的完全图 ...

  3. A. Two Bases

    A. Two Bases time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. Necklace(树状数组+离线操作)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3874 Necklace Time Limit: 15000/5000 MS (Java/Others) ...

  5. CentOS 6.5 搭建 Zabbix

    CentOS 6.5 搭建 Zabbix 说明: 操作系统:CentOS 6.5 IP地址:192.168.21.127 Web环境:Nginx+MySQL+PHP zabbix版本:Zabbix 2 ...

  6. maven学习3,如何创建一个web项目

      Maven学习 (三) 使用m2eclipse创建web项目   1.首先确认你的eclipse已经安装好m2eclipse的环境,可以参照上两篇Maven学习内容 2.新建一个maven的项目 ...

  7. 在 .NET中,一种更方便操作配置项的方法

    在应用程序的开发过程中,我们往往会为软件提供一些配置项,以允许软件根据配置项灵活来做事情,比如配置日志文件路径等,此外,我们还可以用配置项来为用户存储其偏好设置等. .NET 为我们默认提供了配置机制 ...

  8. Spring整合Struts的两种方式介绍

    1 使用Spring托管Struts Action 该种方式就是将Struts Action也视为一种Bean交给Spring来进行托管,使用时Struts的配置文件中配置的Action的classs ...

  9. Codeforces 900C Remove Extra One 模拟

    题目链接:900C Remove Extra One 题意: 首先record是指这个数比数列前面的所有数都大,给了n个数(1-n),删掉一个数,让整个数列的record值达到最大. 题解: 刚开始我 ...

  10. ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(三)—用户账户及cookie配置

    修改用户账户及cookie配置 一.修改密码强度和用户邮箱验证规则: 打开Startup.cs,找到public void ConfigureServices(IServiceCollection s ...