Close Threading

Phil Hornby phil.hornby at accutest.co.uk
Mon Feb 2 12:55:50 CET 2004


Jose,

>> I start the threading process with  t.start()
>> How can i do to close the threading process?

You have several options - depending on the structure of your code - how you
are using your threads. Not claiming any are the best or only ways....but
they are the ways I have used...

If for example you are using event to trigger a thread you could have an
event to trigger ending the thread - and use waitformultipleobjects from
win32all (or equivalent) and then simply allow your run() method to end.

e.g.

class mytheard(threading.Thread):
	def run():
		# do some init
		self.dosomeinit()

		while 1:
			obj = waitformultipleobjects() # needs some params i think...

			if obj == shutdown_obj:
				break

			else:
				# do something
				self.dosomething()

		# do some shutdown
		self.dosomeshutdown()

	def end():
		shutdown_obj.set()

Or

If you have a thread that is generally doing something  then you can use a
simple flag.

e.g.

class mytheard(threading.Thread):
	def run():
		# do some init
		self.dosomeinit()

		while self.continue == 1:
			# do something
			self.dosomething()

			sleep(0)

		# do some shutdown
		self.dosomeshutdown()

...in some other code...
t.continue = 0
...

Or

If you have a thread that runs in the background and you just want it to end
when the main thread does you could just make it a daemon thread like so:

e.g.
class mytheard(threading.Thread):
	def __init__():
		#do some init
		self.dosomeinit()

		# ensure thread is destroyed with the main process
        	self.setDaemon(1)

HTH

--
Phil

"Requirements - what are they I just hack something together that does what
I think they want" ;)

--
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list