python thread的join与setDeamon
join
t.start()
t.join()
Wait until the thread terminates.
This blocks the calling thread until the thread whose join() method called terminates -- either normally or through an unhandled or until the optional timeout occurs.
将子线程join之后,主线程会等待这些join的子线程结束之后才结束.
join会阻塞
for tid in range(2):
t = threading.Thread(target=my_counter)
t.start()
t.join()
上面这段代码其实不该算是多线程.从上面引用的原文描述就说过: Wait until the thread terminates.
上面的代码当执行到join的时候,程序就阻塞住了...,它在等待当前这个子线程执行完成之后才开始下一个循环,开始start下一个子线程.这是顺序执行的.
正确使用join的方式
thread_array = {}
for tid in range(2):
t = threading.Thread(target=my_counter)
t.start()
thread_array[tid] = t
for i in range(2):
thread_array[i].join()
要让所有的t.start()
调用之后再去join它们.
setDeamon
setDeamon设置为True之后,主线程退出,主线程的所有子线程都会被迫退出.
setDeamon设置为False之后,主线程退出,主线程的所有子线程依然可以继续运行.
setDeamon必须在线程start之前设置.
有join的情况下,主线程会等待它的子线程完成之后再退出,自然setDeamon设置成True或者False都无关紧要.
python thread的join与setDeamon的更多相关文章
- python thread的join方法解释
python的Thread类中提供了join()方法,使得一个线程可以等待另一个线程执行结束后再继续运行.这个方法还可以设定一个timeout参数,避免无休止的等待.因为两个线程顺序完成,看起来象一个 ...
- Python Thread related
1.Thread.join([timeout]) Wait until the thread terminates. This blocks the calling thread until the ...
- TLS 与 python thread local
TLS 先说TLS( Thread Local Storage),wiki上是这么解释的: Thread-local storage (TLS) is a computer programming m ...
- python里的Join函数
用法是将数组里的每个元素用字符连接起来 import string string.join(["aaaa", "bbb"]) 或者: from string i ...
- Thread线程join方法自我理解
Thread线程join方法自我理解 thread.join():等待thread线程运行终止,指的是main-thread(main线程)必须等待thread线程运行结束,才能继续thread.jo ...
- 8.Thread的join方法
1.Thread类的join方法表示:当前线程执行结束再执行其它线程!在Thread类中有三个重载的方法分别是: public final synchronized void join(long mi ...
- c++11中关于std::thread的join的思考
c++中关于std::thread的join的思考 std::thread是c++11新引入的线程标准库,通过其可以方便的编写与平台无关的多线程程序,虽然对比针对平台来定制化多线程库会使性能达到最大, ...
- Thread 的join方法
package com.cn.test.thread; public class TestJoin extends Thread{ private String name; public TestJo ...
- Java线程:CountDownLatch 与Thread 的 join()
需求: 主程序中需要等待所有子线程完成后 再继续任务 两种实现方式: 一种使用join() 方法:当在当前线程中调用某个线程 thread 的 join() 方法时,当前线程就会阻塞,直到thread ...
随机推荐
- springboot(4)Druid作为项目数据源(添加监控)
参考博客:恒宇少年:https://www.jianshu.com/p/e84e2709f383 Druid简介 Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JD ...
- 一个文本框的andriod教程
https://blog.csdn.net/androidmsky/article/details/49870823
- 小书MybatisPlus第9篇-常用字段默认值自动填充
本文为Mybatis Plus系列文章的第9篇,前8篇访问地址如下: 小书MybatisPlus第1篇-整合SpringBoot快速开始增删改查 小书MybatisPlus第2篇-条件构造器的应用及总 ...
- Salt组件之管理对象Target
管理对象 Target 在Master上我们可以采用不同Target去管理不同的Minion.这些Target都是通过去管理和匹配Minion的ID来做的一些集合. 1.正则匹配,参数-E,你可以写任 ...
- HTML自动刷新页面
<meta http-equiv="refresh"content="5"/> 英文""
- pandas巩固
导包 import pandas as pd 设置输出结果列对齐 pd.set_option('display.unicode.ambiguous_as_wide',True) pd.set_opti ...
- Python for循环通过序列索引迭代
Python for 循环通过序列索引迭代: 注:集合 和 字典 不可以通过索引进行获取元素,因为集合和字典都是无序的. 使用 len (参数) 方法可以获取到遍历对象的长度. 程序: strs = ...
- PHP timezone_offset_get() 函数
------------恢复内容开始------------ 实例 返回相对于 GMT 的时区偏移: <?php$tz=timezone_open("Asia/Taipei" ...
- PHP getimagesizefromstring - 获取图片信息函数
getimagesizefromstring — 从字符串中获取图像尺寸信息.高佣联盟 www.cgewang.com 语法 array getimagesizefromstring ( string ...
- PDOStatement::errorInfo
PDOStatement::errorInfo — 获取跟上一次语句句柄操作相关的扩展错误信息(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0) 说明 语法 array ...