safest way to kill a thread
Dear all, in python, a thread can be created by t = threading.Thread. But i found that when the main (and the thread) program is running and user use Crtl+C/Crtl+Z to break the program abnormally, the thread is still running and needed to kill manually (by the pid). Is there had any safest way to kill/exit the thread program under python (when the thread program part is a forever loop)? Thank a lot -- http://mail.python.org/mailman/listinfo/python-list
Re: safest way to kill a thread
limodou wrote: >Using Thread's method setDaemon() before you call the start() method. >Just like : >t.setDaemon(True) >t.start() thank for fast reply. from python.org doc, said that setDaemon() function as "The entire Python program exits when no active non-daemon threads are left." is it mean that when the main program exit (normally/abnormally), all threads created will also exit? Thank again. -- http://mail.python.org/mailman/listinfo/python-list
Re: safest way to kill a thread
Great thank for your helping. Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or 0/FALSE to do such action? limodou wrote: >I think only those threads which invoked with setDaemon() method will >exit, and others will not, as the main program exit. -- http://mail.python.org/mailman/listinfo/python-list
Re: safest way to kill a thread
Thank for all helping and sorry that i overlooked the previous message. Peter Hansen wrote: > [EMAIL PROTECTED] wrote: > > Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or > > 0/FALSE to do such action? > > First of all, it's "True" and "False" in Python, not TRUE > and FALSE. > > Secondly, the answer to the question was in the previous > message where "limodou" told you about this in the first > place. Go back and read it again... > > -Peter -- http://mail.python.org/mailman/listinfo/python-list
check socket alive
Dear all, following are some piece of my code (mainly create a socket connection to server and loop to receive data): # function to create and return socket def connect(): server_config = ('192.168.1.50', ); sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect() except socket.error: print "Failed to connect" return 0 return sock # function to receive data def recv_for_sock(sock): sock.settimeout(25) while 1: if sock is None: return 1 try: recv_data = sock.recv(65535) if recv_data: # do something except socket.timeout: print "Socket Timeout" time.sleep (10) pass except socket.error: print "Socket Error" time.sleep (10) pass # main function if __name__ == '__main__': sock = connect() if sock: errorno = recv_for_sock(sock) ... # other stuffs else: print "Cannot create connection" ... my question is, when the socket (create a main function) is disconnected by server (netstat status show close_wait), in "recv_for_sock" function, it can catch the timeout exception at first time. After then, the program will looping/hang within the "recv_for_sock" function (actually is the socket.recv function) and causing CPU usage to 99%. So, how to detect the socket connection is closed at this case? I tried to use exception/check socket is None but no help. Thank for helping. -- http://mail.python.org/mailman/listinfo/python-list
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_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 -- http://mail.python.org/mailman/listinfo/python-list
Re: thread end and make main program end also?
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 -- http://mail.python.org/mailman/listinfo/python-list