?==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 模块详解的更多相关文章

  1. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  2. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  3. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  4. python标准库介绍——31 threading 模块详解

    threading 模块 (可选) ``threading`` 模块为线程提供了一个高级接口, 如 [Example 3-1 #eg-3-1] 所示. 它源自 Java 的线程实现. 和低级的 ``t ...

  5. python标准库介绍——32 Queue 模块详解

    Queue 模块 ``Queue`` 模块提供了一个线程安全的队列 (queue) 实现, 如 [Example 3-2 #eg-3-2] 所示. 你可以通过它在多个线程里安全访问同个对象. ==== ...

  6. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  7. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  8. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  9. python标准库介绍——23 UserString 模块详解

    ==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...

随机推荐

  1. 杂货&&心跳

    https://github.com/jsfront/month/blob/master/2016/201605.md https://github.com/abdmob/x2js https://l ...

  2. 查看JAVA的class二进制文件的方法

    hexdump -C filename可以查看二进制文件. 比如java的Test.java public class Test{ public static void main(String[] a ...

  3. Sqoop拒绝连接错误

    使用Sqoop远程连接MySQL导入数据到HBase数据库: sqoop import --driver com.mysql.jdbc.Driver --connect "jdbc:mysq ...

  4. python获取系统开机时间

    import psutil import time time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(psutil.boot_time()))

  5. Maven项目继承与聚合

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6628534.html  一:继承 在Java编程中,如果多个类都使用到了相同的内容.方法时,我们可以用继承的方 ...

  6. cocos2d-js 和 createjs 性能对比(HTML5)

    cocos2d-js除了做native游戏外,还可以用来做HTML5游戏/动画,那么它跟adobe的createjs框架比较会怎么样呢? (背景知识:createjs是adobe支持的HTML5框架, ...

  7. Flash和js交互的效率分析

    Flash和js交互的效率分析   AS代码: var time:int = getTimer(); for (var i:int = 0; i < 50000; i++) { External ...

  8. Golang Json文件解析为结构体工具-json2go

    代码地址如下:http://www.demodashi.com/demo/14946.html 概述 json2go是一个基于Golang开发的轻量json文件解析.转换命令行工具,目前支持转换输出到 ...

  9. WinPE无法识别NVMe SSD硬盘,如何重装系统

    (源自网络出处不详) 抽风,diy一台新机器,下载的win10系统安装时出现如题所示的问题,开始以为是主板的问题设置u盘启动也不行,后来在某个群里有人说是系统版本问题,无奈重新做了启动优盘(用的17年 ...

  10. HDUOJ-----1074 Integer Inquiry

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...