threading - race condition?

John Nagle nagle at animats.com
Fri May 9 03:12:09 EDT 2008


skunkwerk wrote:
> i'm getting the wrong output for the 'title' attributes for this
> data.  the queue holds a data structure (item name, position, and list
> to store results in).  each thread takes in an item name and queries a
> database for various attributes.  from the debug statements the item
> names are being retrieved correctly, but the attributes returned are
> those of other items in the queue - not its own item.  however, the
> model variable is not a global variable... so i'm not sure what's
> wrong.
> 
> i've declared a bunch of worker threads (100) and a queue into which
> new requests are inserted, like so:
> 
> queue = Queue.Queue(0)
>  WORKERS=100
> for i in range(WORKERS):
> 	thread = SDBThread(queue)
> 	thread.setDaemon(True)
> 	thread.start()
> 
> the thread:
> 
> class SimpleDBThread ( threading.Thread ):
>    def __init__ ( self, queue ):
> 		self.__queue = queue
> 		threading.Thread.__init__ ( self )
>    def run ( self ):
> 		while 1:
> 			item = self.__queue.get()
> 			if item!=None:
> 				model = domain.get_item(item[0])
> 				logger.debug('sdbthread item:'+item[0])
> 				title = model['title']
> 				scraped = model['scraped']
> 				logger.debug("sdbthread title:"+title)
> 
> any suggestions?
> thanks

   Hm.  We don't have enough code here to see what's wrong.
For one thing, we're not seeing how items get put on the queue.  The
trouble might be at the "put" end.

   Make sure that "model", "item", "title", and "scraped" are not globals.
Remember, any assignment to them in a global context makes them a global.

   You should never get "None" from the queue unless you put a "None"
on the queue.  "get()" blocks until there's work to do.

					John Nagle



More information about the Python-list mailing list