473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

thread end and make main program end also?

Dear all,

Following are some codes:

from myClass import * # some user define classes, which will catch
the exception within its function

thread_function():
myClass myclass
while (1):
returnValue = myclass.myfunction();
print "Return Value %s" % returnValue
#... cont' to do something

# main
thrd = threading.Thread(None,thread_function,"thread_func tion")
thrd.setDaemon(True)
thrd.start()
#... cont' to do other thing

'myClass.myfunction()' will return some value (used 'return') when
caught exceptions and let the 'thread_function()' handle the following.
But now found that after 'myClass.myfunction()' return, both thread
program and main program will exit, when i prefer it will cont' to run.
is it the default behavior of thread return? Thank a lot

Jul 18 '05 #1
2 1681
> is it the default behavior of thread return? Thank a lot

No.

First of all: your code is clearly _not_ exposing the described behaviour -
you talk about exception handling, but there is none done. And you don't
say anything about what your doing in your "cont'" parts.

If code run in a thread throws an exception that is not handled, the thread
will die.

The program will terminate when the main thread terminates and all other
threads that have setDaemon(False) are terminated. If all the threads
(except main) have setDaemon(True) (as yours has), the program will end
immediately after the main thread terminated.
Read the threading modules docs.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Sorry that i had't show my code clearly.
The exception try and catch at the module function (i.e.
myClass.myfunction(), which like:

#### start code within myClass.py ####
def myfunction(self, dbconnection):
sql_query= 'update table set col=value'
try:
dbconnection.query(sql_query)
result = dbconnection.store_result()
tuple_list = result.fetch_row(0,0)
except MySQLError (errno, strerror):
print "MySQLError, errno: %s, strerror: %s" % (errno, strerror)
return 11
except (errno, strerror): # catch all
print "General Error, errno: %s, strerror: %s" % (errno, strerror)"
return 21
#### end code within myClass.py ####

and let main thread function (thread_function()) to handle the sub
thread function by its return value (mainly are update database,
printing message and continuous to run).

Should the exception catch at main thread function rather than
class function?
Thank for helping.

return 1

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bruce Bon | last post by:
The class below is intended to play a Sun audio file (.au) in the background while the main thread, which is servicing a GUI, continues without impact. It doesn't work. For a sound file that...
9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
4
by: Alexander | last post by:
Hi, I have written a program that takes on some operations much more time than I expected. As I have seen users clicking wildly on the screen to make something happen, I want to follow the...
1
by: Tom | last post by:
First of all, is it 'legal' (i.e. thread-safe) to pass events back from a worker thread to the main thread? I.E. Can one put PUBLIC EVENT XYZ in their worker thread and then raise that event for...
12
by: Andrew Bullock | last post by:
Hi, I'm using a second thread within my program to do some long calculations, without locking up the UI: worker = new Thread(new ThreadStart(myClass.Run)); worker.Start(); I want to be...
0
by: fiefie.niles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while...
5
by: zxo102 | last post by:
Hi, I am doing a small project using socket server and thread in python. This is first time for me to use socket and thread things. Here is my case. I have 20 socket clients. Each client send a...
5
by: nospam | last post by:
Hi all. I have encountered a "Cross-thread operation not valid" error in a test program I created and although I have came up with a solution I don't like the performance of the program. I...
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.