469,352 Members | 2,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

stop a thread safetely

Hi,

I have a wxPython application that call makes a thread (with threading
module). In some moment i've to stop the thread but i need to finish a
funtion in the thread before it can stop. How can i achive this?

Thanks in advance

Zunbletz
Jul 19 '05 #1
5 2884
Zunbeltz Izaola wrote:
I have a wxPython application that call makes a thread (with threading
module). In some moment i've to stop the thread but i need to finish a
funtion in the thread before it can stop. How can i achive this?


How did you intend to stop the thread in a manner which might be unsafe?
(Hint, unless you're doing something unusual, you can't.)

-Peter
Jul 19 '05 #2
On Fri, 13 May 2005 09:10:13 -0400, Peter Hansen wrote:

How did you intend to stop the thread in a manner which might be unsafe?
(Hint, unless you're doing something unusual, you can't.)

I have a threaded object (Mythread). It checks if want_thread
variable is True to return. The problem is that this object
execute a function that is a tcp comunication

def Client(self,Request,Answer):
totalsent = 0
while totalsent < 608:
sent = self.sock.send(Request.struct2string()[totalsent:])
if sent == 0:
raise RuntimeError, "socket broken"
totalsent = totalsent + sent

if Request.Codigo != 37:
self.WriteLog("Request",Request)
data = self.sock.recv(608)
Answer.string2struct(data)
if int(Answer.Param[9]) != 37:
self.WriteLog("Answer",Answer)
The Client function send a Request (them write it in a log file),
gets and answer and and write it. The problem is that when i stop
the thread a get somethime the Request writed but not the answer,
as if the funciton Client returns before it ends.

Zunbeltz
-Peter


Jul 19 '05 #3
Zunbeltz Izaola wrote:
On Fri, 13 May 2005 09:10:13 -0400, Peter Hansen wrote:
How did you intend to stop the thread in a manner which might be unsafe?
(Hint, unless you're doing something unusual, you can't.)
I have a threaded object (Mythread). It checks if want_thread
variable is True to return. The problem is that this object
execute a function that is a tcp comunication


Since you didn't include any of the code related to threads, I'll limit
my analysis to the code you did post:
def Client(self,Request,Answer):
totalsent = 0
while totalsent < 608:
sent = self.sock.send(Request.struct2string()[totalsent:])
if sent == 0:
raise RuntimeError, "socket broken"
totalsent = totalsent + sent
Note that you could simplify the above code to this one line:
self.sock.sendall(Request.struct2string())
if Request.Codigo != 37:
self.WriteLog("Request",Request)
data = self.sock.recv(608) ^^^^^^^^^^^^^^^^^^^^^^^^^^ BAD CODE
Answer.string2struct(data)
if int(Answer.Param[9]) != 37:
self.WriteLog("Answer",Answer)

The Client function send a Request (them write it in a log file),
gets and answer and and write it. The problem is that when i stop
the thread a get somethime the Request writed but not the answer,
as if the funciton Client returns before it ends.


This is possibly consistent with the error marked above as BAD CODE. A
call to recv() does not guarantee that the full 608 bytes of data is
read. It could actually read any amount from 1 to 608 bytes. See the
docs and/or Google for "python socket howto" for more detail.

Note also that so far it still looks like this has nothing to do with
threads. If you believe it does, you'll need to provide more background
and code.

-Peter
Jul 19 '05 #4
Peter Hansen wrote:
call to recv() does not guarantee that the full 608 bytes of data is


Does read() have that guarantee?
Jul 19 '05 #5
Ivan Voras wrote:
Peter Hansen wrote:
call to recv() does not guarantee that the full 608 bytes of data is


Does read() have that guarantee?


Assuming you mean read() on the file object that would be returned by
calling makefile() on the socket, then the docs imply that this is the
case, but I wouldn't call that a "guarantee". Someone who knows the
inner workings might have a comment on that (in which case it might make
a good patch to the docs to clarify this issue...).

-Peter
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Sacha | last post: by
reply views Thread by andrewcw | last post: by
4 posts views Thread by Osvaldo Bisignano | last post: by
3 posts views Thread by Ha ha | last post: by
1 post views Thread by magic man via .NET 247 | last post: by
6 posts views Thread by Tomaz Koritnik | last post: by
2 posts views Thread by many_years_after | last post: by
3 posts views Thread by yeye.yang | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.