473,500 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3046
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
3454
by: Sacha | last post by:
Consider I have a working thread like this, while (1) { if ( g_pObject ) g_pObject->DoOneStep(); } with an class hierarchie like this
0
1003
by: andrewcw | last post by:
I have an external VB6 app that I call in a loop. Because the process is long, I would like to be able to terminate the spawned thread & the VB6 application. My current loop calls a this...
4
3327
by: Osvaldo Bisignano | last post by:
When I press a button, I start a new Exe file as a new Thread. When the mainform of that exe closes, i thought the thread was over. When and How shoud i terminate the subprocess?
3
5944
by: Ha ha | last post by:
I create a main thread ,than create a subthread in the main thread,when i found any error,i want to stop the subthread and the main thread immediatly .How can i do for it. *** Sent via...
1
2230
by: magic man via .NET 247 | last post by:
hi everyone i have a c# application that uses multithreading toconnect to sql server and execute a stored procedure on theserver. i am using a dataset,sqlcommand,dataadapter and adatagrid to carry...
6
23703
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100);...
1
1526
by: scorpion53061 | last post by:
I have MS Word operating in a thread other than the main writing a report. Can I tell the main thread to wait until a particular point (a sub starts) in another thread before continuing on?
2
3134
by: many_years_after | last post by:
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...
3
3166
by: yeye.yang | last post by:
hey everybody Does everybody can help me or give me some advise for the cross thread exception catch Here is what I want to do: I have 2 classes "Scenario" and "Step", which have a...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.