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

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

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

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

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

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

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

  4. python标准库介绍——33 thread 模块详解

    ?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Dubbo源码解读:appendAnnotation [01]

    import java.lang.reflect.Method; public class AppendAnnotation { /** * 获取某个Annotation实例的所有方法值(实际是Ann ...

  2. Artistic Style在windows下的使用(C/C++)

    ArtisticStyle是一个开源的源码格式化工具.主页地址为:http://astyle.sourceforge.net/,它能够应用在C.C++.Objective-C.C#.Java等程序语言 ...

  3. django之创建第7-1个项目-url配置高级

    修改urls.PY文件 # -*- coding: UTF-8 -*- from django.conf.urls import patterns, include, url # Uncomment ...

  4. mysql workbench图形化mysql管理工具

    MYSQL官网也推出了针对Linux的图形化的连接工具-MySQL Workbench.MySQL Workbench不仅仅是一个简单的MySQL客户端.简而言之,Workbench是一个跨平台的 ( ...

  5. Retrofit、Okhttp使用小记(cookie,accesstoken,POST

    博主在项目中用RxJava也差不多几个月了,但是结合Retrofit使用经验还不是太多.恰好新项目的后台是http+json的,就打算尝试一把. 刚开始由于Retrofit还不太熟,但是后台接口急着测 ...

  6. AWVS扫描工具使用教程

    上文AppScan扫描工具-工作原理&操作教程有写到Web安全漏洞扫描工具之一的APPScan,除此,还有另外一款国内使用比较主流的Web安全漏洞扫描工具——AWVS.相较于大容量的AppSc ...

  7. ios中项目

    上一个版本 http://pan.baidu.com/share/link?shareid=2183244558&uk=923776187 下载地址  http://pan.baidu.com ...

  8. python web中的并发请求

    python web可以选择django,也可以选择flask,它们的原理差不多.flask比较轻量,下面写一段flask程序来说明python web对并发请求的处理. app.py import ...

  9. 将windows目录共享到linux

    1.将windows目录共享 2.安装cifs 3.  mount -t cifs -o username=电脑登陆用户名,password=电脑登陆用户密码 //127.0.0.1/abc /var ...

  10. Event事件的兼容性(转)

    window.event问题问题说明:window.event 只能在IE下运行,而不能在Firefox下运行,这是因为Firefox的event只能在事件发生的现场使用.解决方法:在事件发生的函数上 ...