使用多线程处理列队数据。这样的话,就可以不断的给子线程发送数据由子线程处理了。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Queue import
import
import
class
def
threading.Thread.__init__(self, name=t_name)
self.data=queue
self.condition = cond
def
while(True):
self.condition.acquire()
if not
xtmp = self.data.get()
print 'size thread=',self.data.qsize()
print 'qeue=%d'%(xtmp)
self.condition.release()
#Main thread
def
queue = Queue()
con = threading.Condition()
'Con.', queue,con)
True)
consumer.start()
while(True):
2)
if
99)
98)
97)
print 'size main=',queue.qsize()
print 'xxxx'
print 'All threads terminate!'
if __name__ == '__main__':
main()
输出结果:
size main=size thread= 2
qeue=99
size thread= 1
2qeue=98
size thread= 0
xxxx
qeue=97
size main=size thread= 22
qeue=99
size thread= 1
qeue=98
size thread= 0
qeue=97
xxxx
size main= 3size thread=
xxxx
2
qeue=99
size thread= 1
qeue=98
size thread= 0
qeue=97
size main= 3size thread= 2
qeue=99
。。。