如何我使用的元组设置变量传递到线程队列使用Python建立一个队列
ml =[('A', '2011-04', '2011-05'), ('b', '2011-07', '2011-04', '2011-05'), ('c', '2011-06', '2011-07', '2011-04', '2011-05')]
以下是queing设置:
# build a queue with tuples
queue = Queue.Queue()
for row in ml:
if not row or row[0] == "#":
continue
qSplit = row.split()
queue.put((qSplit[0], qSplit[1], qSplit[2],qSplit[3]))
更新在这里更多的代码:
import sys, threading, Queue
ml = [('a', '2011-04', '2011-05'), ('b', '2011-07', '2011-04', '2011-05'), ('c', '2011-06', '2011-07', '2011-04', '2011-05')]
class WorkerThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while 1:
try: # take a job from the queue
id, null, null2, null3 = self.queue.get_nowait()
except Queue.Empty:
raise SystemExit
if __name__ == '__main__':
connections = 5
# build a queue with tuples
queue = Queue.Queue()
for row in ml:
if not row or row[0] == "#":
continue
queue.put(row[:3])
# print queue
threads = []
for dummy in range(connections):
t = WorkerThread(queue)
t.start()
threads.append(t)
# wait for all threads to finish
for thread in threads:
thread.join()
sys.stdout.write("\n")
sys.stdout.flush()
这是什么都与多线程呢? – 2011-04-01 17:39:40