471,092 Members | 987 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

Try to stop thread Using flag


I countered some problems when I try to stop threads using flag.
These are my some important codes:

##### mythread.py
def run(self):
while self.addr != '': ### text waiting for processing
if not self.CONTINUE: ### flag for thread ,means to exit
the RUN func or not
print self.name
post(self.params, self.addr) ### func to process the text
self.addr = furl.readline().strip()
###### myapp.py
def OnEndProcess(self, event):
if self.threadList:
for thread in self.threadList:
thread.CONTINUE = False
##### this func is an EVENT processor. When I press the END BUTTON ,
it will be caused.

def OnBeginProcess(self, event):
for i in range(num):
if lines[i]!= '':
thread =
mythread.mythread('Thread'+str(i),lines[i], params)

print '\n Starting Threads'

for i in self.threadList:
###### this func is an EVENT processor. When I press the BEGIN BUTTON
, it will be caused.

By test I found this truely acts as supposed:thread exited the *run()*
func. But when I press the BEGIN BUTTON again, an Error was caused:
thread already started.
It's said that if the *run()* func is finished, the thread will become
dead. So how to interprete this?
Any solution?


Nov 25 '06 #1
2 3061



Michel Claveau
Nov 25 '06 #2
Hash: SHA1

The Python documentation states:

start( )
Start the thread's activity.

This must be called at most once per thread object. It arranges for
the object's run() method to be invoked in a separate thread of control.
So you cannot start() one Thread object a second time, you have to
create a new one. But as you don't move any thread out of your list in
the OnEndProcess function all stopped Thread objects are indeed
restarted by your OnBeginProcess as they are still in self.threadlist.

Furthermore: you should wait in our OnEndProcess function and check
whether all thread have ended indeed by calling Thread.join().
Otherwise some threads mights still be running indeed.

Hope I could make it clear.


Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

Nov 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Simon Niederberger | last post: by
3 posts views Thread by Sunny | last post: by
26 posts views Thread by Ricardo | last post: by
7 posts views Thread by Ivan | last post: by
3 posts views Thread by Ha ha | last post: by
51 posts views Thread by Hans | last post: by
3 posts views Thread by Saizan | last post: by
18 posts views Thread by J.K. Baltzersen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.