title: Globally-scoped variable: can its value change before it is picked up by the thread?
body: <p>In the following code, you see that <code>pickledList</code> is being used by the thread and is set in the global scope. </p>
  
  <p>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 <strong>make sure</strong> that its value doesn't change before the thread gets to use it?</p>
  
  <pre><code>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() )
  </code></pre>
  
  <p>EDIT: </p>
  
  <p>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:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>How can I pass the value in <code>i</code> to the thread in such a way that its no longer bound to the variable, so that if the value stored in <code>i</code> changes, the value the thread is working with is not affected.</p>