1. Queue 模块
  2.  
  3. ``Queue`` 模块提供了一个线程安全的队列 (queue) 实现, [Example 3-2 #eg-3-2] 所示.
  4. 你可以通过它在多个线程里安全访问同个对象.
  5.  
  6. ====Example 3-2. 使用 Queue 模块====[eg-3-2]
  7.  
  8. ```
  9. File: queue-example-1.py
  10.  
  11. import threading
  12. import Queue
  13. import time, random
  14.  
  15. WORKERS = 2
  16.  
  17. class Worker(threading.Thread):
  18.  
  19. def _ _init_ _(self, queue):
  20. self._ _queue = queue
  21. threading.Thread._ _init_ _(self)
  22.  
  23. def run(self):
  24. while 1:
  25. item = self._ _queue.get()
  26. if item is None:
  27. break # reached end of queue
  28.  
  29. # pretend we're doing something that takes 10?00 ms
  30. time.sleep(random.randint(10, 100) / 1000.0)
  31.  
  32. print "task", item, "finished"
  33.  
  34. #
  35. # try it
  36.  
  37. queue = Queue.Queue(0)
  38.  
  39. for i in range(WORKERS):
  40. Worker(queue).start() # start a worker
  41.  
  42. for i in range(10):
  43. queue.put(i)
  44.  
  45. for i in range(WORKERS):
  46. queue.put(None) # add end-of-queue markers
  47.  
  48. *B*task 1 finished
  49. task 0 finished
  50. task 3 finished
  51. task 2 finished
  52. task 4 finished
  53. task 5 finished
  54. task 7 finished
  55. task 6 finished
  56. task 9 finished
  57. task 8 finished*b*
  58. ```
  59.  
  60. [Example 3-3 #eg-3-3] 展示了如何限制队列的大小. 如果队列满了,
  61. 那么控制主线程 (producer threads) 被阻塞, 等待项目被弹出 (pop off).
  62.  
  63. ====Example 3-3. 使用限制大小的 Queue 模块====[eg-3-3]
  64.  
  65. ```
  66. File: queue-example-2.py
  67.  
  68. import threading
  69. import Queue
  70.  
  71. import time, random
  72.  
  73. WORKERS = 2
  74.  
  75. class Worker(threading.Thread):
  76.  
  77. def _ _init_ _(self, queue):
  78. self._ _queue = queue
  79. threading.Thread._ _init_ _(self)
  80.  
  81. def run(self):
  82. while 1:
  83. item = self._ _queue.get()
  84. if item is None:
  85. break # reached end of queue
  86.  
  87. # pretend we're doing something that takes 10?00 ms
  88. time.sleep(random.randint(10, 100) / 1000.0)
  89.  
  90. print "task", item, "finished"
  91.  
  92. #
  93. # run with limited queue
  94.  
  95. queue = Queue.Queue(3)
  96.  
  97. for i in range(WORKERS):
  98. Worker(queue).start() # start a worker
  99.  
  100. for item in range(10):
  101. print "push", item
  102. queue.put(item)
  103.  
  104. for i in range(WORKERS):
  105. queue.put(None) # add end-of-queue markers
  106.  
  107. *B*push 0
  108. push 1
  109. push 2
  110. push 3
  111. push 4
  112. push 5
  113. task 0 finished
  114. push 6
  115. task 1 finished
  116. push 7
  117. task 2 finished
  118. push 8
  119. task 3 finished
  120. push 9
  121. task 4 finished
  122. task 6 finished
  123. task 5 finished
  124. task 7 finished
  125. task 9 finished
  126. task 8 finished*b*
  127. ```
  128.  
  129. 你可以通过继承 //Queue// 类来修改它的行为. [Example 3-4 #eg-3-4]
  130. 为我们展示了一个简单的具有优先级的队列. 它接受一个元组作为参数,
  131. 元组的第一个成员表示优先级(数值越小优先级越高).
  132.  
  133. ====Example 3-4. 使用 Queue 模块实现优先级队列====[eg-3-4]
  134.  
  135. ```
  136. File: queue-example-3.py
  137.  
  138. import Queue
  139. import bisect
  140.  
  141. Empty = Queue.Empty
  142.  
  143. class PriorityQueue(Queue.Queue):
  144. "Thread-safe priority queue"
  145.  
  146. def _put(self, item):
  147. # insert in order
  148. bisect.insort(self.queue, item)
  149.  
  150. #
  151. # try it
  152.  
  153. queue = PriorityQueue(0)
  154.  
  155. # add items out of order
  156. queue.put((20, "second"))
  157. queue.put((10, "first"))
  158. queue.put((30, "third"))
  159.  
  160. # print queue contents
  161. try:
  162. while 1:
  163. print queue.get_nowait()
  164. except Empty:
  165. pass
  166.  
  167. *B*third
  168. second
  169. first*b*
  170. ```
  171.  
  172. [Example 3-5 #eg-3-5] 展示了一个简单的堆栈 (stack) 实现
  173. (末尾添加, 头部弹出, 而非头部添加, 头部弹出).
  174.  
  175. ====Example 3-5. 使用 Queue 模块实现一个堆栈====[eg-3-5]
  176.  
  177. ```
  178. File: queue-example-4.py
  179.  
  180. import Queue
  181.  
  182. Empty = Queue.Empty
  183.  
  184. class Stack(Queue.Queue):
  185. "Thread-safe stack"
  186.  
  187. def _put(self, item):
  188. # insert at the beginning of queue, not at the end
  189. self.queue.insert(0, item)
  190.  
  191. # method aliases
  192. push = Queue.Queue.put
  193. pop = Queue.Queue.get
  194. pop_nowait = Queue.Queue.get_nowait
  195.  
  196. #
  197. # try it
  198.  
  199. stack = Stack(0)
  200.  
  201. # push items on stack
  202. stack.push("first")
  203. stack.push("second")
  204. stack.push("third")
  205.  
  206. # print stack contents
  207. try:
  208. while 1:
  209. print stack.pop_nowait()
  210. except Empty:
  211. pass
  212.  
  213. *B*third
  214. second
  215. first*b*
  216. ```

python标准库介绍——32 Queue 模块详解的更多相关文章

  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标准库介绍——30 code 模块详解

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

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

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

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

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

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

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

  8. python标准库介绍——22 UserList 模块详解

    ==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...

  9. python标准库介绍——21 UserDict 模块详解

    ==UserDict 模块== ``UserDict`` 模块包含了一个可继承的字典类 (事实上是对内建字典类型的 Python 封装). [Example 2-15 #eg-2-15] 展示了一个增 ...

随机推荐

  1. MDX Step by Step 读书笔记(九) - Working with Time 处理时间

    开篇介绍 这一章节主要用到的 MDX 函数: PeriodsToDate( [Level , [Member]] ) - 从指定级别的范围内,返回与指定成员同一级别,从第一个期间开始到指定成员结束的期 ...

  2. windows 2012授权模型

    转自:http://www.aidanfinn.com/?p=13090 Remember that Microsoft licenses its servers at the physical le ...

  3. C++ 第八课 标准c字符和字符串

    atof() 将字符串转换成浮点数 atoi() 将字符串转换成整数 atol() 将字符串转换成长整型数 isalnum() 当字母或数字字符时, 返回真值 isalpha() 当字母字符时, 返回 ...

  4. LNMP一键安装包-CentOS/Ubuntu/Debian自动安装Nginx,MySQL,PHP

    适用环境: 系统支持:CentOS.Ubuntu.Debian 内存要求:≥128M 安装了什么: 1.Nginx-1.2.1 2.MySQL 5.5.25 3.PHP 5.2.17或PHP 5.3. ...

  5. 【BZOJ2631】tree

    Description 一棵n个点的树.每一个点的初始权值为1. 对于这棵树有q个操作,每一个操作为下面四种操作之中的一个: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 ...

  6. 用户从输入URL到看到网页发生了什么?

    一.在浏览器地址栏输入URL 二.浏览器查看缓存1.如果资源未缓存,发起新请求如果已缓存,检验是否处于有效期,资源处于有效期内直接提供给客户端,否则与服务器进行验证.2.检验有效期通常有两个HTTP头 ...

  7. Run Test Case on Spark

        今天有哥们问到怎样对Spark进行单元測试.如今将Sbt的測试方法写出来,例如以下:     对Spark的test case进行測试的时候能够用sbt的test命令:     一.測试所有t ...

  8. 通过 Apache Commons HttpClient 发送 HTTPS 请求

    1.通过 HTTPS 发送 POST 请求: 2.HTTPS 安全协议采用 TLSv1.2: 3. 使用代理(Proxy)进行 HTTPS 访问: 4.指定 Content-Type 为:applic ...

  9. Java泛型的PECS原则

    1.什么是PESC ? PESC  = producer-extens , consumer -super. 如果参数化类型表示一个 T 生产者,就使用 <? extends T>: 如果 ...

  10. great tips in soapui

    from this site :http://onebyteatatime.wordpress.com/2009/04/18/soapui-tips-n-tricks-part-2/