python标准库介绍——31 threading 模块详解
threading 模块 (可选) ``threading`` 模块为线程提供了一个高级接口, 如 [Example 3-1 #eg-3-1] 所示.
它源自 Java 的线程实现. 和低级的 ``thread`` 模块相同, 只有你在编译解释器时打开了线程支持才可以使用它 . 你只需要继承 //Thread// 类, 定义好 ``run`` 方法, 就可以创建一 个新的线程.
使用时首先创建该类的一个或多个实例, 然后调用 ``start`` 方法. 这样每个实例的
``run`` 方法都会运行在它自的线程里. ====Example 3-1. 使用 threading 模块====[eg-3-1] ```
File: threading-example-1.py import threading
import time, random class Counter:
def _ _init_ _(self):
self.lock = threading.Lock()
self.value = 0 def increment(self):
self.lock.acquire() # critical section
self.value = value = self.value + 1
self.lock.release()
return value counter = Counter() class Worker(threading.Thread): def run(self):
for i in range(10):
# pretend we're doing something that takes 10?00 ms
value = counter.increment() # increment global counter
time.sleep(random.randint(10, 100) / 1000.0)
print self.getName(), "-- task", i, "finished", value #
# try it for i in range(10):
Worker().start() # start a worker *B*Thread-1 -- task 0 finished 1
Thread-3 -- task 0 finished 3
Thread-7 -- task 0 finished 8
Thread-1 -- task 1 finished 7
Thread-4 -- task 0 Thread-5 -- task 0 finished 4
finished 5
Thread-8 -- task 0 Thread-6 -- task 0 finished 9
finished 6
...
Thread-6 -- task 9 finished 98
Thread-4 -- task 9 finished 99
Thread-9 -- task 9 finished 100*b*
``` [Example 3-1 #eg-3-1] 使用了 //Lock// 对象来在全局 //Counter// 对象里创建临界区
(critical section). 如果删除了 ``acquire`` 和 ``release`` 语句, 那么 ``Counter`` 很可能不会到达 100.
python标准库介绍——31 threading 模块详解的更多相关文章
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——33 thread 模块详解
?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...
- 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标准库介绍——32 Queue 模块详解
Queue 模块 ``Queue`` 模块提供了一个线程安全的队列 (queue) 实现, 如 [Example 3-2 #eg-3-2] 所示. 你可以通过它在多个线程里安全访问同个对象. ==== ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
随机推荐
- openfiler在esxi下的安装配置
注意分区的时候如果硬盘太小自动分区会导致分配的卷大小不够用 后改为如下: 以root登录: 应该以openfiler登录,口令是password 也可以导入虚拟机安装 升级虚拟机硬件版本 终端登录用户 ...
- 算法笔记_199:第二届蓝桥杯软件类决赛真题(C语言本科)
前言:以下代码部分仅供参考,C语言解答部分全部来自网友,Java语言部分部分参考自网友,对于答案的正确性不能完全保证. 试题1 数论中有著名的四方定理:所有自然数至多只要用四个数的平方和就可以表示. ...
- web.xml关于spring的讲解
<context-param>的作用: web.xml的配置中<context-param>配置作用 . 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件w ...
- MongoDB分片配置系列一:
接这篇博客: http://www.cnblogs.com/xiaoit/p/4479066.html 这里不再说明安装过程. 1:分片简介 分片是一种将海量的数据水平扩展的数据库集群系统,数据分表存 ...
- Tensorflow设置显存自适应,显存比例
1. 按比例 config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.4 session = ...
- ORA-00942 表或视图不存在
场景:跨scheme创建视图,提示ORA-00942 表或视图不存在 1. 创建两个用户 CREATE USER ODI_SRC IDENTIFIED BY ODI_SRC CREATE USER O ...
- 多表连接的三种方式详解 hash join、merge join、 nested loop
在多表联合查询的时候,如果我们查看它的执行计划,就会发现里面有多表之间的连接方式.多表之间的连接有三种方式:Nested Loops,Hash Join 和 Sort Merge Join.具体适用哪 ...
- Java DESede 加解密("DESede/ECB/PKCS5Padding")
private static final Cipher DES_CIPHER; static { try { DES_CIPHER = Cipher.getInstance("DESede/ ...
- exception java.lang.OutOfMemoryError: Java heap space
1.情景展示 java内存溢出异常,将程序代码问题排除在外,如何增大JVM的使用内存? 2.解决方案 在eclipse中的解决办法:增大你要运行的测试类的内存分配. 点击运行或debug按钮旁的 ...
- maven正确的集成命令-U-B
http://healthandbeauty.iteye.com/blog/1618501 在持续集成服务器上使用怎样的 mvn 命令集成项目,这个问题乍一看答案很显然,不就是 mvn clean i ...