Question

I would like to understand a little bit better the mechanics of assignments in multithreaded programs.

In the following snippet of code, what stops each thread from assigning a new value to item before any thread has managed to put send the value to q_out?

import Queue, threading

q_in = Queue.Queue()
q_out = Queue.Queue()

def worker():
    while True:
        item = q_in.get()
        q_out.put(do_work(item))
        q_in.task_done()

I would have thought I would need to do something like the following, which starts a class instance for each thread with its own set of values, but I gather this isn't really necessary?

import Queue, threading

q_in = Queue.Queue()
q_out = Queue.Queue()

class Worker(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.item = None

    def run(self):
        self.item = q_in.get()
        q_out.put(do_work(self.item))
        q_in.task_done()

So I assume there is either a python mechanic I don't understand or there is some kind of built in functionality to the threading or Queue modules?

Was it helpful?

Solution

In your first code example, if you have multiple threads and they each call worker() then each thread has a stack on which item is created and each item refers to a different element of q_in.

Also, just to be clear, the function do_work() cannot modify what item refers to.

In your second snippet, you have essentially move item out of the stack and onto the heap where it is associated with an instance of Worker.

In both cases each thread has its own item which refers to different instances simultaneously.

Also, for novice programmers, you should consider passing q_in and q_out into the function worker() or each instance of a Worker rather than having them as global variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top