In the following code, you see that pickledList is being used by the thread and is set in the global scope.
If the variable that the thread was using was set dynamically somewhere down below in that final while loop, is it possible that its value could change before the thread got to use it? How can I set a value dynamically in the 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
someList = [ 1, 2, 7, 9, 0 ]
pickledList = pickle.dumps ( someList )
class ClientThread ( threading.Thread ):
def run ( self ):
while True:
client = clientPool.get()
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 ]
clientPool = Queue.Queue ( 0 )
for x in xrange ( 2 ):
ClientThread().start()
server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
server.bind ( ( '', 2727 ) )
server.listen ( 5 )
while True:
clientPool.put ( server.accept() )
EDIT:
Here is 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 is not affected.
Option 1: you can pass arguments into each Thread when it is instantiated:
in which case your
runmethod will be called: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
ClientThreadobjects: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
runis first called and store a copy locallyOption 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.