z()是一个非常快速的生成器,比do()处理它们的速度要快。有没有办法将“results”对象排队?这个物体有什么属性?这就是我做的,我在内部设置了一个队列来排队‘结果’,有没有更好的方法
from multiprocessing.dummy import Pool as ThreadPool
import Queue,time,random
from datetime import datetime
class rob:
def __init__(self,maxq):
self.count = 0
self.q = Queue.Queue(maxq)
self.maxq = maxq
def add(self,x=1):
self.count += x
def z(self):
while True:
#s = random.choice(range(1,6))
#time.sleep(.1)
t = datetime.now().strftime("%I:%M:%S.%f")
self.add()
t = t+'---'+str(self.count)
#print 'z:',t
if self.q.full():
print 'waiting'
self.q.put(t)
yield t
def do(self,t):
secs = random.choice(range(1,10))
#could take max of 10 seconds per do()
time.sleep(secs)
d = self.q.get()
return t
#print e.message, e.args
#patch_send()
mt = 100
b = rob(mt*3)
pool = ThreadPool(mt) #because u can
results = pool.imap_unordered( b.do, b.z() )
#pool.close()
for r in results:
print r
目前没有回答
相关问题 更多 >
编程相关推荐