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.