转载-- python queue interpretation

Original Article: http://blog.csdn.net/crazy_fire/article/details/7506239

今天学习了下Queue,本来是按照Python核心编程第二版上的学的,但是那个上面介绍的及其的简单,我这样一个对进程本来就不熟悉的新手来说简直就是一个挑战,再三研究之后重要搞懂了。搞懂了就是爽啊,看起来,用起来都简单了呵呵。

    Queue是标准模块所以可以直接import,用起来就像是个真正的队列,先进先出的原则,这样进程间就可以用这个数据了,丫丫的,这个可好了。还是那个经典的生产者和消费者关系开始吧,书上讲的不通俗,记得记得学MFC时候那个孙鑫就介绍的比较容易接受。好了,切入主题。

   有一个生产线,一个消费线,那么我们要做的就是消费的不能比生产的要多。脚本如下:

  1. #!/usr/bin/env python  
  2.   
  3. ''''' 
  4. Created on Apr 25, 2012 
  5.  
  6. @author: stedy 
  7. '''  
  8. #coding=cp936  
  9. from random import randint  
  10. from time import sleep,ctime  
  11. from Queue import Queue  
  12. import threading  
  13.        
  14. class MyThread(threading.Thread):  
  15.     def __init__(self,func,args,name=""):  
  16.         threading.Thread.__init__(self)  
  17.         self.name = name  
  18.         self.func = func  
  19.         self.args = args  
  20.     def getResult(self):  
  21.         return self.res  
  22.     def run(self):  
  23.         print 'stating',self.name,'at',ctime()  
  24.         self.func(*self.args)  
  25.           
  26. def writeQ(queue):  
  27.     i = randint(1,100)  
  28.     print 'producing object for Q...',queue.put(i,1)  
  29.           
  30. def readQ(queue):  
  31.     if queue.empty() == False:  
  32.         val = queue.get(1)  
  33.         print 'value from Q',val  
  34.     else:  
  35.         print "no value,wait a moment"  
  36.         sleep(1)  
  37.              
  38.        
  39. def writer(queue,loops):  
  40.     for i in range(loops):  
  41.         writeQ(queue)  
  42.         sleep(randint(1,3))  
  43.        
  44. def reader(queue,loops):  
  45.     for i in range(loops):  
  46.         readQ(queue)  
  47.         sleep(randint(1,3))  
  48.        
  49. funcs = [reader,writer]  
  50. nfuncs = range(len(funcs))  
  51.        
  52. def main():  
  53.     nloops = 99  
  54.     q = Queue(100)  
  55.          
  56.     threads = []  
  57.     for i in nfuncs:  
  58.         t = MyThread(funcs[i],(q,nloops),funcs[i].__name__)  
  59.         threads.append(t)  
  60.       
  61.     for i in nfuncs:  
  62.         threads[i].start()  
  63.          
  64.     for i in nfuncs:  
  65.         threads[i].join()  
  66.     print 'all Done'  
  67.        
  68. if __name__ == '__main__':  
  69.     main()  



stedy: 第一个例子的运行结果为


  1. stating reader at Wed Apr 25 21:55:03 2012  
  2. no value,wait a moment  
  3. stating writer at Wed Apr 25 21:55:03 2012  
  4. producing object for Q... None  
  5. value from Q 65  
  6. producing object for Q... None  
  7. value from Q 94  
  8. producing object for Q... None  
  9. producing object for Q... None  
  10. value from Q 80  
  11. producing object for Q... None  
  12. value from Q 6  
  13. producing object for Q... None  
  14. value from Q 17  
  15. producing object for Q... None  
  16. producing object for Q... None  
  17. value from Q 11  
  18. producing object for Q... None  
  19. value from Q 33  
  20. producing object for Q... None  
  21. value from Q 46  
  22. producing object for Q... None  
  23. producing object for Q... None  
  24. producing object for Q... None  
  25. value from Q 14  
  26. value from Q 13  
  27. producing object for Q... None  
  28. value from Q 47  
  29. producing object for Q... None  
  30. value from Q 50  
  31. producing object for Q... None  
  32. value from Q 54  
  33. producing object for Q... None  
  34. producing object for Q... None  
  35. value from Q 17  
  36. producing object for Q... None  
  37. producing object for Q... None  
  38. value from Q 44  
  39. value from Qproducing object for Q... None  
  40.  26  
  41. producing object for Q... None  
  42. value from Q 77  
  43. producing object for Q... None  
  44. value from Q 38  
  45. producing object for Q... None  
  46. value from Q 14  
  47. value from Q 29  
  48. producing object for Q... None  
  49. value from Q 58  
  50. producing object for Q... None  
  51. value from Q 71  
  52. producing object for Q... None  
  53. value from Q 47  
  54. producing object for Q... None  
  55. value from Q 70  
  56. producing object for Q... None  
  57. value from Q 69  
  58. producing object for Q...value from Q 20   
  59. None  
  60. producing object for Q... None  
  61. value from Q 22  
  62. producing object for Q... None  
  63. value from Q 96  
  64. producing object for Q... None  
  65. producing object for Q...value from Q  52None  
  66.   
  67. value from Q 63  
  68. producing object for Q... None  
  69. value from Q 79  
  70. producing object for Q... None  
  71. value from Q 99  
  72. value from Q 6  
  73. producing object for Q... None  
  74. value from Q 36  
  75. producing object for Q... None  
  76. value from Q 64  
  77. producing object for Q... None  
  78. value from Q 26  
  79. producing object for Q... None  
  80. value from Q 37  
  81. producing object for Q... None  
  82. value from Q 13  
  83. producing object for Q... None  
  84. value from Q 41  
  85. producing object for Q... None  
  86. value from Q 20  
  87. producing object for Q... None  
  88. value from Q 33  
  89. value from Q 26  
  90. producing object for Q... None  
  91. value from Q 21  
  92. producing object for Q... None  
  93. value from Q 50  
  94. value from Q 33  
  95. producing object for Q... None  
  96. value from Q 45  
  97. producing object for Q... None  
  98. value from Q 31  
  99. producing object for Q... None  
  100. producing object for Q... None  
  101. value from Q 39  
  102. producing object for Q... None  
  103. value from Q 90  
  104. producing object for Q... None  
  105. producing object for Q... None  
  106. value from Q 78  
  107. producing object for Q... None  
  108. producing object for Q... None  
  109. value from Q 84  
  110. producing object for Q... None  
  111. producing object for Q... None  
  112. value from Q 44  
  113. producing object for Q... None  
  114. value from Q 78  
  115. value from Q 17  
  116. producing object for Q... None  
  117. value from Q 7  
  118. value from Q 7  
  119. producing object for Q... None  
  120. value from Q 41  
  121. value from Q 71  
  122. producing object for Q... None  
  123. value from Q 57  
  124. producing object for Q... None  
  125. value from Q 65  
  126. producing object for Q... None  
  127. value from Q 12  
  128. value from Q 1  
  129. producing object for Q... None  
  130. producing object for Q... None  
  131. value from Q 97  
  132. value from Q 65  
  133. producing object for Q... None  
  134. value from Q 48  
  135. producing object for Q... None  
  136. producing object for Q... None  
  137. value from Q 1  
  138. producing object for Q... None  
  139. value from Q 58  
  140. value from Q 62  
  141. producing object for Q... None  
  142. value from Q 91  
  143. producing object for Q... None  
  144. value from Q 87  
  145. producing object for Q... None  
  146. value from Q 92  
  147. producing object for Q... None  
  148. value from Q 23  
  149. value from Q 15  
  150. producing object for Q... None  
  151. producing object for Q... None  
  152. value from Q 77  
  153. producing object for Q... None  
  154. producing object for Q... None  
  155. value from Q 3  
  156. value from Q 33  
  157. producing object for Q... None  
  158. producing object for Q... None  
  159. value from Q 87  
  160. producing object for Q... None  
  161. value from Q 94  
  162. producing object for Q... None  
  163. producing object for Q... None  
  164. value from Q 89  
  165. producing object for Q... None  
  166. value from Q 48  
  167. producing object for Q... None  
  168. producing object for Q... None  
  169. value from Q 65  
  170. producing object for Q... None  
  171. value from Q 55  
  172. producing object for Q... None  
  173. producing object for Q... None  
  174. value from Q 28  
  175. producing object for Q... None  
  176. producing object for Q... None  
  177. value from Q 61  
  178. producing object for Q... None  
  179. value from Q 47  
  180. producing object for Q... None  
  181. value from Q 16  
  182. producing object for Q... None  
  183. value from Q 77  
  184. producing object for Q... None  
  185. value from Q 86  
  186. producing object for Q... None  
  187. value from Q 39  
  188. value from Q 20  
  189. producing object for Q... None  
  190. value from Q 63  
  191. value from Q 13  
  192. producing object for Q... None  
  193. producing object for Q... None  
  194. value from Q 57  
  195. producing object for Q... None  
  196. value from Q 12  
  197. value from Q 9  
  198. value from Q 42  
  199. value from Q 70  
  200. value from Q 65  
  201. all Done  














    我们提取Queue的部分来看,首先是创建一个队列这是一个全局变量,当然是main内的’全局‘,这个变量给谁用呢?当然是给各个线程的函数用了,这里的数据就是能够共享的数据。

    存入数据的函数是put简单吧,直接把数据给放入进去,取出的就是get也简单啊,不管你在函数的什么地方用都可以看到这两个函数那么我们开始担心的问题其实用这连个函数就可以搞定了,就是如果消费的数量多于生产的数量,put(value,block=0)如果我们将第二个参数设置不为0的数据,那么函数就等待到队列中有空间为止,那样线程不就挂起了吗,所以生产商就有时间生产了啊,同样如果get(block=0)函数如果参数不为0同样的等待队列有数据时候才退出。呵呵,这模样,队列这个东西很好用啊

官方文档如下:

Class Queue implements queue objects and has the methods described below. This class can be derived from in order to implement other queue organizations (e.g. stack) but the inheritable interface is not described here. See the source code for details. The public methods are:

qsize( )
Return the approximate size of the queue. Because of multithreading semantics, this number is not reliable.
empty( )
Return True if the queue is empty, False otherwise. Because of multithreading semantics, this is not reliable.
full( )
Return True if the queue is full, False otherwise. Because of multithreading semantics, this is not reliable.
put( item[, block[, timeout]])
Put item into the queue. If optional args block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. Otherwise ( block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception ( timeout is ignored in that case).

New in version 2.3: the timeout parameter.

put_nowait( item)
Equivalent to put(item, False).
get( [block[, timeout]])
Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise ( block is false), return an item if one is immediately available, else raise the Empty exception ( timeout is ignored in that case).

New in version 2.3: the timeout parameter.

get_nowait( )
Equivalent to get(False).

Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.

task_done( )
Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.

If a join() is currently blocking, it will resume when all items have been processed (meaning that atask_done() call was received for every item that had beenput() into the queue).

Raises a ValueError if called more times than there were items placed in the queue.New in version 2.5.

join( )
Blocks until all items in the queue have been gotten and processed.

The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread callstask_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.New in version 2.5.

Example of how to wait for enqueued tasks to be completed:



  1. def worker():   
  2.         while True:   
  3.             item = q.get()   
  4.             do_work(item)   
  5.             q.task_done()   
  6.   
  7.     q = Queue()   
  8.     for i in range(num_worker_threads):   
  9.          t = Thread(target=worker)  
  10.          t.setDaemon(True)  
  11.          t.start()   
  12.   
  13.     for item in source():  
  14.         q.put(item)   
  15.   
  16.     q.join()       # block until all tasks are done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值