【发布时间】:2014-06-14 21:41:13
【问题描述】:
假设我有两个队列,分别叫它们colors 和numbers:
colors = Queue.Queue()
numbers = Queue.Queue()
它们每个都包含几个项目:
for color in ['red', 'orange', 'yellow', 'green', 'blue', 'indago', 'violet']:
colors.put(color)
for i in xrange(20):
numbers.put(i)
还有一个处理数字和字母组合的函数:
def handle():
while not colors.empty()
color = colors.get()
number = numbers.get()
print "Foo: %s bar: %d" % (color, number)
colors.task_done()
numbers.task_done()
将被线程化:
children = []
for i in xrange(num_threads):
children.append(threading.Thread(target=handle))
但我想打印所有可能的颜色和数字组合,而不是只打印每种颜色和数字,最有效的方法是什么? 这是我希望输出的样子:http://pastebin.com/yhksKswr
问题(我想是很好的功能)是Queue.get() 删除了它从队列中返回的项目,以便每个项目只使用一次。
【问题讨论】:
-
使用
itertools。你描述的问题似乎与Queue无关。 -
@BrianCain 我使用
Queue的原因是我不必担心线程安全。我知道如何处理列表,但不知道队列。
标签: python multithreading performance python-2.7 queue