使用python构建队列
如何使用元组设置变量以传递到线程队列
ml =[('A', '2011-04', '2011-05'), ('b', '2011-07', '2011-04', '2011-05'), ('c', '2011-06', '2011-07', '2011-04', '2011-05')]
下面是排队设置:
# 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()
How to I use Tuples to setup variables to pass into a threading queue
ml =[('A', '2011-04', '2011-05'), ('b', '2011-07', '2011-04', '2011-05'), ('c', '2011-06', '2011-07', '2011-04', '2011-05')]
Below is the queing setup:
# 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] ))
UPDATE here some more code:
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定我是否理解这个问题。
行不通,因为
row
是一个元组,而不是一个字符串。但似乎您要做的就是将元组拆分为其组件,然后立即将它们粘在一起形成一个新元组。这对我来说没有意义。您也没有在
for
循环中执行任何操作。怎么样:
不需要分割任何东西......
Not sure I understand the problem.
isn't going to work because
row
is a tuple, not a string. But it seems all you're trying to do is to split the tuple into its components, only to stick them together into a new tuple right away. That doesn't make sense to me.You're also not doing anything in your
for
loop.How about this:
No need to split anything...
如果您唯一的问题是您只想要每个元组的前几个元素(我不太确定您想要什么),那么您可以使用切片表示法来删除多余的元素。这还可以防止元素少于四个的行由
row[3]
引发的 IndexError。If your only problem is that you want only the first few elements of each tuple (I'm not quite sure what you want), then you can use slice notation to remove the extra elements. This also prevents the IndexError that would arise from
row[3]
for rows with fewer than four elements.