views:

367

answers:

1

okay, I'm learning how to pool threads in python. I'm relatively new to threading in general and I have a question. In the following code, you see that the pickledList variable thats being used by the thread is set in the global scope. This is an example from a howto on a website. My question is: what if the variable that the thread was using was set dynamically somewhere down below in that final while loop. Is it not possible that its value could change before the thread got to use it? how can I set a value dynamically down there in that loop, send it off to the thread and MAKE SURE that its value doesn't change before the thread gets to use it?

import pickle
import Queue
import socket
import threading

# We'll pickle a list of numbers, yet again:
someList = [ 1, 2, 7, 9, 0 ]
pickledList = pickle.dumps ( someList )

# A revised version of our thread class:
class ClientThread ( threading.Thread ):

   # Note that we do not override Thread's __init__ method.
   # The Queue module makes this not necessary.

   def run ( self ):

      # Have our thread serve "forever":
      while True:

         # Get a client out of the queue
         client = clientPool.get()

         # Check if we actually have an actual client in the client variable:
         if client != None:

            print 'Received connection:', client [ 1 ] [ 0 ]
            client [ 0 ].send ( pickledList )
            for x in xrange ( 10 ):
               print client [ 0 ].recv ( 1024 )
            client [ 0 ].close()
            print 'Closed connection:', client [ 1 ] [ 0 ]

# Create our Queue:
clientPool = Queue.Queue ( 0 )

# Start two threads:
for x in xrange ( 2 ):
   ClientThread().start()

# Set up the server:
server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
server.bind ( ( '', 2727 ) )
server.listen ( 5 )

# Have the server serve "forever":
while True:
   clientPool.put ( server.accept() )

EDIT:

heres a better example of my problem. If you run this, sometimes the values change before the thread can output them causing some to be skipped

from threading import Thread
class t ( Thread ):
    def run(self):
        print "(from thread) ",
        print i

for i in range(1, 50):    
    print i
    t().start()

how can I pass the value in i to the thread in such a way that its no longer bound to the variable. So that if the value stored in i changes, the value the thread is working with isnt affected.

+1  A: 

Option 1: you can pass arguments into each Thread when it is instantiated:

ClientThread(arg1, arg2, kwarg1="three times!").start()

in which case your run method will be called:

run(arg1, arg2, kwarg1="three times!")

by the Thread instance when you call start(). If you need to pass mutable objects (dicts, lists, instances) to the function, you must be sure to re-assign the global variable, not edit it in place.

Option 2: you can set an instance variable on your ClientThread objects:

myThread.setMyAttribute('new value')

With option 2 you need to be wary of race conditions etc. depending on what the method does. Using Locks whenever you're writing or reading data from/to a thread is a good idea.

Option 3: grab the global when run is first called and store a copy locally

run(self):
    localVar = globalVar # only for immutable types
    localList = globalList[:] # copy of a list
    localDict = globalDict.copy() # Warning! Shallow copy only!

Option 1 is the right way to go if the value a thread is given never needs to change during its lifetime, Option 2 if the value does need to change. Option 3 is a hack.

With regard to generally passing variables/values around, you must remember with Python that immutable objects (strings, numbers, tuples) are passed by value and mutable objects (dicts, lists, class instances) are passed by reference.

Consequently, altering a string, number or tuple will not affect any instances previously passed that variable, however altering a dict, list or instance will do. Re-assigning a variable to a different object will not affect anything previously given the old value.

Globals certainly shouldn't be used for values that may change (if at all). Basically your example is doing it the wrong way.

wbg
Thanks for baring wiht me there :) very helpful answer.
Ryan
All cleared up?
wbg
Okay, your answer makes perfect sense but it brings up another question. I'm using option 1, but I don't know how to pass those values to the thread when using Queueing.
Ryan
The threads are being created and added to the pool before any of the relevant data to be put into the threads is even generated. Is that okay? Can I pass the data into those threads afterwards somehow using Queue to put that data into a thread thats already into the thread pool?
Ryan
You could do that with a Queue, but you don't really need a queue for that.If passing in the value at thread creation time isn't sufficient, you should use option 2.
wbg