By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

thread end and make main program end also?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
> 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.