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. vue css 模块化编程 CSS Modules Scoped

    1.scoped https://vue-loader.vuejs.org/zh/guide/scoped-css.html 2.module https://vue-loader.vuejs.org ...

  2. Java多线程之线程的状态以及线程间协作通信导致的线程状态转换

      转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6561589.html  一:线程的状态以及变化图 Java中线程中状态可分为五种:New(新建状态),Ru ...

  3. Java多线程之创建线程的三种方式比较

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6560057.html  一:继承Thread类创建线程 1:继承Thread类定义线程子类: 2:重写run( ...

  4. 〖Android〗dropbear一些操作命令备忘

    相关命令行: # 启动dropbear后台运行 /data/local/tmp/dropbear \ -A -N android -I -C -G -p \ -R /data/local/tmp/au ...

  5. V-rep学习笔记:main script and child scripts

    The main and child scripts The main script and the child scripts, which are simulation scripts, play ...

  6. Javascript Get or Set Checked Radio Value

    Description This pair of Javascript function can get or set the checked value of a group of radio bu ...

  7. mingw 构建 mysql-connector-c-6.1.9记录

    1.准备工作 首先需要下载mysql-connector-c-6.1.9的源码,然后解压. 然后需要准备编译环境,这里我使用的是msys2(下载地址http://repo.msys2.org/dist ...

  8. vijos 1006 晴天小猪历险记之Hill——数字三角形的终极变化

    题目链接:https://vijos.org/p/1006 数字三角形原题看这里:http://www.cnblogs.com/huashanqingzhu/p/7326837.html 背景 在很久 ...

  9. Mysql中字符串正确的连接方法

    虽然SQL server和My sql的语句基本都一致,但是仍然存在一些小区别.就如字符串的连接来说,SQL server中的字符串连接是使用“+”来连接,不带引号sql server是做加法运算.而 ...

  10. Gson转换json数据为对象

    可以通过Gson使用两种方法,将json字符串转换为对象,以下面该段报文做测试 { "id": 84041462, "lastName": "小华&q ...