[Tutor] Variables in Threaded functions

lonetwin lonetwin <lonetwin@yahoo.com>
Thu, 11 Oct 2001 21:58:56 +0530 (IST)


Hi all ye good people,
     I'm kinda new to threaded programming in python (in any language
 for that matter !!). I'd like somebody to clear this up for me.
	 I want to write a class that inherits from threading.Thread,
 whose run() function runs a loop that looks at a self.sequence,
 if there is something left in that sequence it goes and does
 something(self.sequence.pop()). Now a limited (say 3) threads would
 execute. My understanding is (correct me if I'm wrong) all the threads
 share the same self.sequence (if they don't how do I make them share a
 sequence ....without having to put in a global sequence ??) and also
 I'm uncertain about the implications of doing such things (shud I use
 locks ?? ...if I shud....how do I do that ??)
    Basically I want someone to explain how this code works and the
implications of doing such things on a larger scale:

========================================================================

import threading
import time
class Strand(threading.Thread):
	bag = range(100)
	def __init__(self):
		threading.Thread.__init__(self)

	def run(self):
		while len(self.bag):
			print self.bag.pop(),

def main():
	Rope = []

	for x in range(3):
		thread = Strand()
		Rope.append(thread)

	for	thread in Rope:
		thread.start()

	print done

if __name__ == '__main__':
	main()
-- 
----------------------------------------------
Every oak tree started out as a
couple of nuts who stood their ground.
                                    Anonymous
----------------------------------------------