2

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()
1
  • What does this have to do with multithreading? Commented Apr 1, 2011 at 17:39

3 Answers 3

1

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...

Sign up to request clarification or add additional context in comments.

1 Comment

id, null, null, null3 = self.queue.get_nowait() ValueError: need more than 3 values to unpack.....Some tup have 3 parameters some have 5, want only 4 or less......
1

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])

Comments

0
# 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.