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// . 前者是对标准字符串类型的 ...
随机推荐
- Centos7中修改Hostname的方法
一.Centos7中修改的方法: hostnamectl set-hostname <new hostname> 说明:centOS 7 里面修改hostname的方式有所改变,修改/et ...
- 1064: 不明飞行物(ufo)
#include<iostream> #include<string> using namespace std; int main(){ string a,b ; long m ...
- Linq中的连接(join)
http://www.cnblogs.com/scottckt/archive/2010/08/11/1797716.html Linq中连接主要有组连接.内连接.左外连接.交叉连接四种.各个用法如下 ...
- sqlmap检测sql注入漏洞
sqlmap是一款非常强大的开源sql自动化注入工具,可以用来检测和利用sql注入漏洞.它由python语言开发而成,因此运行需要安装python环境. 官网:http://sqlmap.org/ 乌 ...
- 【Python】文件读写操作
Python的文件读写有点类似php的文件读写.php的文件读写已经在<[php]让记事本成为你调控变量的控制台>(点击打开链接)说过了,以下用一个小样例说明Python的文件读写. 在F ...
- Eclipse远程连接HBase
在Eclipse下新建一个Map/Reduce项目,并将以下jar添加到Build path: 程序代码: package thathbase; import java.io.IOException; ...
- 定制保存top输出信息的格式详解
top命令的重要性和使用方法不多说了,这里终点讨论如何保存top命令的输出信息. 保存top命令的输出到一个文件的方法是:top -n1b > topinfo.txt,这没什么好奇的,但 ...
- Generate BKS File( Bouncy Castle KeyStore)
echo "Enter BKS output file name : \c" read filename echo "Enter BKS Password : \c&qu ...
- 详解 KDTree
https://blog.csdn.net/qing101hua/article/details/53228668
- 〖Android〗简单隐藏Android虚拟键盘的方法
在Android使用 input 输入文本时,不期望出现虚拟键盘: 一般情况下,大家会把键盘给卸载,不过也有简单的方法: 就是下载一个 com.wparam.nullkeyboard (NullKey ...