thread end and make main program end also?

M

martinnitram

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_function")
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
 
D

Diez B. Roggisch

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.
 
M

martinnitram

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
 

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

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,140
Members
47,755
Latest member
Grazynkaa

Latest Threads

Top