使用python构建队列

发布于 2024-10-28 17:16:40 字数 1480 浏览 1 评论 0原文

如何使用元组设置变量以传递到线程队列

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城花音 2024-11-04 17:16:40

不确定我是否理解这个问题。

qSplit = row.split()

行不通,因为 row 是一个元组,而不是一个字符串。但似乎您要做的就是将元组拆分为其组件,然后立即将它们粘在一起形成一个新元组。这对我来说没有意义。

您也没有在 for 循环中执行任何操作。

怎么样:

for row in ml:
    if not row or row[0] == "#":
        continue
    queue.put(row)

不需要分割任何东西......

Not sure I understand the problem.

qSplit = row.split()

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:

for row in ml:
    if not row or row[0] == "#":
        continue
    queue.put(row)

No need to split anything...

究竟谁懂我的在乎 2024-11-04 17:16:40

如果您唯一的问题是您只想要每个元组的前几个元素(我不太确定您想要什么),那么您可以使用切片表示法来删除多余的元素。这还可以防止元素少于四个的行由 row[3] 引发的 IndexError。

for row in ml:
    if not row or row[0]== "#":
        continue
    queue.put(row[:4])

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.

for row in ml:
    if not row or row[0]== "#":
        continue
    queue.put(row[:4])
分分钟 2024-11-04 17:16:40
# build a queue with tuples
queue = Queue.Queue()
for row in ml:
    if not row or row[0] == "#":
        continue
    for item in row[:4]:
        q.put(item)
# build a queue with tuples
queue = Queue.Queue()
for row in ml:
    if not row or row[0] == "#":
        continue
    for item in row[:4]:
        q.put(item)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文