By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Try to stop thread Using flag

P: n/a
Hi,pythoners:

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
break
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)
self.threadList.append(thread)

print '\n Starting Threads'

for i in self.threadList:
i.start()
###### 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?

Thanks.

Nov 25 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
MC

http://candygram.sourceforge.net/

--
@-salutations

Michel Claveau
Nov 25 '06 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
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.

Greetings

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

iD8DBQFFaHbrRQeuB6ws8wERAr4+AJ0UOTCPZoJLK8F4AfnfKy PInwQLVQCcC3a6
6aBP+POkMkKCym6JRaP87yw=
=AgnH
-----END PGP SIGNATURE-----
Nov 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.