python标准库介绍——33 thread 模块详解
?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示.
只有你在编译解释器时打开了线程支持才可以使用它. 如果没有特殊需要, 最好使用高级接口 ``threading`` 模块替代. ====Example 3-6. 使用 thread 模块====[eg-3-6] ```
File: thread-example-1.py import thread
import time, random def worker():
for i in range(50):
# pretend we're doing something that takes 10?00 ms
time.sleep(random.randint(10, 100) / 1000.0)
print thread.get_ident(), "-- task", i, "finished" #
# try it out! for i in range(2):
thread.start_new_thread(worker, ()) time.sleep(1) print "goodbye!" *B*311 -- task 0 finished
265 -- task 0 finished
265 -- task 1 finished
311 -- task 1 finished
...
265 -- task 17 finished
311 -- task 13 finished
265 -- task 18 finished
goodbye!*b*
``` 注意当主程序退出的时候, 所有的线程也随着退出. 而 ``threading`` 模块不存在这个问题 . (该行为可改变)
python标准库介绍——33 thread 模块详解的更多相关文章
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——31 threading 模块详解
threading 模块 (可选) ``threading`` 模块为线程提供了一个高级接口, 如 [Example 3-1 #eg-3-1] 所示. 它源自 Java 的线程实现. 和低级的 ``t ...
- python标准库介绍——32 Queue 模块详解
Queue 模块 ``Queue`` 模块提供了一个线程安全的队列 (queue) 实现, 如 [Example 3-2 #eg-3-2] 所示. 你可以通过它在多个线程里安全访问同个对象. ==== ...
- python标准库介绍——30 code 模块详解
==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...
- python标准库介绍——8 operator 模块详解
==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...
- python标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
随机推荐
- 1051: 手机(MOBILE)
#include <iostream> #include <iomanip> #include <cstdlib> #include <string> ...
- llvm code call graph
https://www.ics.usi.ch/images/stories/ICS/slides/llvm-graphs.pdf
- Jquery 一些好用的插件和工具类
1.做页面校验的工具类 <!--area.js存放区域编码的一个常量.由于bee.js里面的getPersonInfo18()方法需要调用这个常量,所以在bee.js之前引入.如果不需要用到这个 ...
- JSjs获取当前时间的前一天/后一天(昨天/明天)
Date curDate = new Date(); var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天 var next ...
- Influxdb数据压缩
环境: CentOS6.5_x64InfluxDB版本:1.1.0 数据压缩可以参考: https://docs.influxdata.com/influxdb/v1.1/concepts/stora ...
- Java的PriorityQueue
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6538654.html 优先队列实质上就是数据结构中的最小堆,而堆从概念图来看类似于一棵二叉树,从具体实现来说 ...
- JavaWeb项目配置化之Properties类的使用
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6397370.html 在项目中的应用见: https://github.com/ygj0930/Couple ...
- Definitaion of 'utsname' must be imported from module 'Darwin.POSIX.sys.utsname' before it is required
https://stackoverflow.com/questions/34430354/objective-c-gettimeofday-must-be-imported
- Java ConcurrentHashMap (Java代码实战-005)
package Threads; import com.google.common.collect.Maps; import java.util.concurrent.ConcurrentMap; i ...
- V-rep学习笔记:vrep中的实用工具
在V-REP的模型浏览器中可以找到一个工具文件夹tools,点开后会在下面一栏中显示许多方框图,将这些方框拖到场景模型中可以实现一些特定的功能,方便建模或其它操作. Center of mass vi ...