python threading模块中的join()方法和setDeamon()方法的一些理解
之前用多线程的时候看见了很多文章,比较常用的大概就是join()和setDeamon()了。
先说一下自己对join()的理解吧:
def join(self, timeout=None):
"""Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is
called terminates -- either normally or through an unhandled exception
or until the optional timeout occurs. When the timeout argument is present and not None, it should be a
floating point number specifying a timeout for the operation in seconds
(or fractions thereof). As join() always returns None, you must call
isAlive() after join() to decide whether a timeout happened -- if the
thread is still alive, the join() call timed out. When the timeout argument is not present or None, the operation will
block until the thread terminates. A thread can be join()ed many times. join() raises a RuntimeError if an attempt is made to join the current
thread as that would cause a deadlock. It is also an error to join() a
thread before it has been started and attempts to do so raises the same
exception. """
源码的__doc__其实说的很清楚,join()会阻塞调用该线程的线程,知道该线程结束(This blocks the calling thread
until the thread whose join() method is called terminates)。
注意点:
#coding:utf-8
import threading
import time def action(arg):
time.sleep(1)
print 'sub thread start!the thread name is:%s ' % threading.currentThread().getName()
print 'the arg is:%s ' %arg
time.sleep(1) #不正确写法,会导致多线程顺序执行,失去了多线程的意义
for i in xrange(4):
t =threading.Thread(target=action,args=(i,))
t.setDaemon(True)
t.start()
t.join() #正确写法
thread_list = [] #线程存放列表
for i in xrange(4):
t =threading.Thread(target=action,args=(i,))
t.setDaemon(True)
thread_list.append(t) for t in thread_list:
t.start() for t in thread_list:
t.join() print 'main_thread end!'
下面再说一下setDeamon()吧:
其实主线程并不会结束setDeamon(True)的线程,而是当主线程执行完毕之后不会再去关注setDeamon(True)的线程。
所以setDeamon(True)的线程的结果是我们无法获取到的,类似于爱咋咋地?不管你输出什么,我都不看,主线程跑
完就结束整个python process。
而setDeamon(False)的线程会一直受到主线程的关注,就算主线程跑完了也会等setDeamon(False)的线程跑完然后
再结束整个python process。
所以说,就算setDeamon(True)的线程在主线程之后跑完,但如果在setDeamon(False)的线程之前跑完的话,也是会
输出结果的,而不是被所谓的主线程结束就杀死setDeamon(False)的线程。
附上我的调试代码,
# -*- coding:utf-8 -*- import threading
import time stime = time.time() def action(arg):
time.sleep(1)
print ('the arg is:%s,time is %s ' % (arg, time.time())) def action1(arg):
time.sleep(1)
print ('the arg is:%s,time is %s ' % (arg, time.time())) thread_list = [] # 线程存放列表
for i in [777, 888]:
t = threading.Thread(target=action, args=(i,))
t.setDaemon(False)
thread_list.append(t)
for i in range(10):
t = threading.Thread(target=action1, args=(i,))
t.setDaemon(True)
thread_list.append(t) for t in thread_list:
t.start() print('**************')
etime = time.time()
print('time cost is %s' % (etime - stime))
python threading模块中的join()方法和setDeamon()方法的一些理解的更多相关文章
- Java8新特性(一)_interface中的static方法和default方法
什么要单独写个Java8新特性,一个原因是我目前所在的公司用的是jdk8,并且框架中用了大量的Java8的新特性,如上篇文章写到的stream方法进行过滤map集合.stream方法就是接口Colle ...
- JS中的call()方法和apply()方法用法总结
原文引自:https://blog.csdn.net/ganyingxie123456/article/details/70855586 最近又遇到了JacvaScript中的call()方法和app ...
- Hibernate中Session.get()方法和load()方法的详细比较
一.get方法和load方法的简易理解 (1)get()方法直接返回实体类,如果查不到数据则返回null.load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用 ...
- js中的splice方法和slice方法简单总结
slice:是截取用的 splice:是做删除 插入 替换用的 slice(start,end): 参数: start:开始位置的索引 end:结束位置的索引(但不包含该索引位置的元素) 例如: va ...
- JS中的call()方法和apply()方法用法总结(挺好 转载下)
最近又遇到了JacvaScript中的call()方法和apply()方法,而在某些时候这两个方法还确实是十分重要的,那么就让我总结这两个方法的使用和区别吧. 1. 每个函数都包含两个非继承而来的方法 ...
- TP框架中的A方法和R方法
ThinkPHP 跨模块调用操作方法(A方法与R方法) 跨模块调用操作方法 前面说了可以使用 $this 来调用当前模块内的方法,但实际情况中还经常会在当前模块调用其他模块的方法.ThinkPHP 内 ...
- Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍
在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...
- java 中的set方法和get方法的理解
get的意思是获取,set的意思是设置. get方法和set方法是实现类的封装访问的很好的工具. 当类中的变量设为private 时,他的意思就是说,只能通过自身和子类的访问,但是对于别的其他的类来说 ...
- java8新特性:interface中的static方法和default方法
java8中接口有两个新特性,一个是静态方法,一个是默认方法. static方法 java8中为接口新增了一项功能:定义一个或者多个静态方法. 定义用法和普通的static方法一样: public i ...
随机推荐
- STM32的PA15、PB3、 PB4管脚作普通管脚的解决办法
最近做了一个板子,使用的是SWD方式进行下载程序,仅仅使用到SWDIO(PA13) 和SWCLK(PA14)两个管脚.我将PA15(JTDI)和PB3(JTDO)管脚用于他用(用于点LED使用), ...
- Xgboost GPU 加速
import xgboost as xgb import numpy as np from sklearn.datasets import fetch_covtype from sklearn.mod ...
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- julia 安装
https://julialang.org/downloads/ 下载安装程序,拖拽完成安装
- lua 5.3.5 安装/初体验
安装 官网http://www.lua.org/start.html 参考 https://blog.csdn.net/qq_23954569/article/details/70879672 cd ...
- python两个列表合并为字典,一个作为key,一个作为value
两个列表合并为一个字典函数list_dic(list1,list2)可以直接复制拿走 传入的参数为两个列表,list1准备作为key,list2准备作为value,key和value位置一一对应. d ...
- windows 安装xadmin
1.访问github :https://github.com/sshwsfc/xadmin 2.新建README.rst 并替换到下载的zip文件中 3.cmd下,进入虚拟环境使用pip instal ...
- C# WPF开发之MVVM模式开发
MVVM模式由Model,View,ViewModel三部分组成. Model需继承INotifyPropertyChange(属性修改通知) ViewModel负责业务逻辑,连接View和Model ...
- 基于springboot的ssm
参考该网址成功搭建: https://blog.csdn.net/liboyang71/article/details/73459909 目前有几个问题: 1.我使用application.yml配置 ...
- 合并两个 Lambda 表达式
概述 在开发工作中,有些时候需要对一些增删改查进行封装(用 Lambda 表达式来筛选数据),但是又有一部分条件总是相同的,对于相同的部分可以直接写到方法里,而不同的部分作为参数传进去. 定义扩展方法 ...