stop a thread safetely

Z

Zunbeltz Izaola

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
 
P

Peter Hansen

Zunbeltz said:
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
 
Z

Zunbeltz Izaola

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
 
P

Peter Hansen

Zunbeltz said:
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
 
P

Peter Hansen

Ivan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,239
Messages
2,571,199
Members
47,835
Latest member
CyrusRuggi

Latest Threads

Top