468,505 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

Try to stop thread Using flag

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
2 2997
MC

http://candygram.sourceforge.net/

--
@-salutations

Michel Claveau
Nov 25 '06 #2
-----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.

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
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.