之前用多线程的时候看见了很多文章,比较常用的大概就是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()方法的一些理解的更多相关文章

  1. Java8新特性(一)_interface中的static方法和default方法

    什么要单独写个Java8新特性,一个原因是我目前所在的公司用的是jdk8,并且框架中用了大量的Java8的新特性,如上篇文章写到的stream方法进行过滤map集合.stream方法就是接口Colle ...

  2. JS中的call()方法和apply()方法用法总结

    原文引自:https://blog.csdn.net/ganyingxie123456/article/details/70855586 最近又遇到了JacvaScript中的call()方法和app ...

  3. Hibernate中Session.get()方法和load()方法的详细比较

    一.get方法和load方法的简易理解  (1)get()方法直接返回实体类,如果查不到数据则返回null.load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用 ...

  4. js中的splice方法和slice方法简单总结

    slice:是截取用的 splice:是做删除 插入 替换用的 slice(start,end): 参数: start:开始位置的索引 end:结束位置的索引(但不包含该索引位置的元素) 例如: va ...

  5. JS中的call()方法和apply()方法用法总结(挺好 转载下)

    最近又遇到了JacvaScript中的call()方法和apply()方法,而在某些时候这两个方法还确实是十分重要的,那么就让我总结这两个方法的使用和区别吧. 1. 每个函数都包含两个非继承而来的方法 ...

  6. TP框架中的A方法和R方法

    ThinkPHP 跨模块调用操作方法(A方法与R方法) 跨模块调用操作方法 前面说了可以使用 $this 来调用当前模块内的方法,但实际情况中还经常会在当前模块调用其他模块的方法.ThinkPHP 内 ...

  7. Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍

    在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...

  8. java 中的set方法和get方法的理解

    get的意思是获取,set的意思是设置. get方法和set方法是实现类的封装访问的很好的工具. 当类中的变量设为private 时,他的意思就是说,只能通过自身和子类的访问,但是对于别的其他的类来说 ...

  9. java8新特性:interface中的static方法和default方法

    java8中接口有两个新特性,一个是静态方法,一个是默认方法. static方法 java8中为接口新增了一项功能:定义一个或者多个静态方法. 定义用法和普通的static方法一样: public i ...

随机推荐

  1. InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [1,136,240,64] vs. shape[1] = [1,135,240,64]

    初始输入图片大小为1600*1200,设置的输入网络的最大测试图片大小为1600*1200相当于scale =1 ,运行没有问题.之后输入图片大小为1920*1080,测试图片大小为1920*1080 ...

  2. intelliJ idea常用快捷键 mac版

    目录 coding project coding Command + P 显示方法参数信息 Command + N 自动生成getter.setter.hashCode.equals.toString ...

  3. Xilinx Vivado的使用详细介绍(5):调用用户自定义封装的IP核

    Zedboard OLED Display Controller IP v1 介绍 Author:zhangxianhe 本文档提供了快速添加,连接和使用ZedboardOLED v1.0 IP内核的 ...

  4. trueStudio笔记

    1.C标准的选择 可以在项目->属性->C/C++ Build->Setting->Tool Setting->C Compiler->General中选择使用不同 ...

  5. 解决JS中取URL地址中的参数中文乱码

    GET请求会将中文编码,如果取出乱码的话,应该进行解码操作, 下面的函数是获取指定参数名的参数值,参数值可是中文.英文. function getQueryString(name) { var reg ...

  6. cowboy源码分析(三)

    上接 cowboy源码分析(二) 我们接着分析cowboy_protocol.erl的request/7模块 -module(cowboy_protocol). %% API.-export([sta ...

  7. Fiddler抓取https请求 & Fiddler抓包工具常用功能详解

    Fiddler抓取https请求 & Fiddler抓包工具常用功能详解   先来看一个小故事: 小T在测试APP时,打开某个页面展示异常,于是就跑到客户端开发小A那里说:“你这个页面做的有问 ...

  8. 【洛谷p1162】填涂颜色

    (今天yy出奇的不活泼,认真的吓人) [传送门] 算法标签: 思路啊qwq: part1: 想法是先暴搜出每一行的1,取最前方一个1和最后方一个1,然后中间的0填上色,80分,因为没有考虑到“0001 ...

  9. PHP获取POST的几种方法

    一.PHP获取POST数据的几种方法 方法1.最常见的方法是:$_POST['fieldname'];说明:只能接收Content-Type: application/x-www-form-urlen ...

  10. 第八届蓝桥杯 4、方格分割 DFS

    标题:方格分割 6x6的方格,沿着格子的边线剪开成两部分. 要求这两部分的形状完全相同. 如图:p1.png, p2.png, p3.png 就是可行的分割法. 试计算: 包括这3种分法在内,一共有多 ...