Thanks.
What have also found that seems nice/usable enough is to sort of implement a
class that inherits from threading.Thread, and then it runs the class's
functionality in the threaded background, and you can define other functions
and variables to check state if you want to - something like the following -
off the top of my head:
#---start of code---
import threading, time
class myClass(threading.Thread):
counter = -1
def __init__(self):
self.counter = 0
threading.Thread.__init__(self)
def getCount(self):
return self.counter
def run(self):
while self.counter < 60:
self.counter += 1
time.sleep(1)
#---end of code---
The partial point is this one will be initiated, but when you then tell your
instance of the class/object to start(), it sort of automatically tells itself
to run(), and you can call the getCount() function to get the value of it's
internal counter variable, etc. - simple example, but seems to work well
enough, if you get my/the drift, and, what have pretty much found all over the
'net is that you get told not to try destroy anything manually/yourself, but
anyway.
Stay well
Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'
----- Original Message -----
From: geoff
To: Jacob Kruger
Cc: [email protected]
Sent: Thursday, October 27, 2011 3:47 PM
Subject: Re: [python-win32] Restart/re-run a thread
The python docs are pretty clear that there is no way to external stop a
thread and this was a design decision.
Typically, I have used a threading.Event to control the actions of the worker
threads from a loop in the main program.
Setup each worker thread as a loop using the event.wait() method to block the
loop. When you are ready for all of the threads to proceed, use the
controller thread and event.set(). All of the worker threads will exit the
blocking state of their loop and proceed with execution. Once finished, they
return to the event.wait() mode.
To stop the threads, I typically use another thread.Event object called
"keepgoing" and test for that at the top of the worker threads processing loop.
When the threads have finished processing and you want them to stop, you can
set the keepgoing event to false.
The example below is from memory, so be careful;
keepgoing = threading.Event()
keepgoing.set() # evaluates to True
startprocess = threading.Event()
startprocess.clear() # evaluates to False
class worker(threading.Thread):
def __init__(self, keepgoing, startprocess):
while keepgoing:
if startprocess:
... do someprocessing here
else:
startprocess.wait() #blocks the thread waiting
for the event to be set.
Note that if you need to pass objects into the worker thread, I would suggest
placing in a collections.deque. One the startprocess is set, you can use
deque.pop() to take the object out of the deque in a thread safe way.
Hope this helps.
BTW, I am not sure if this is for education purposes or for production, but
debugging threads is REALLY HARD, so make sure this is something you really
need to do.
_______________________________________________
python-win32 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-win32