如下代码:当线程ReqSender抛异常退出时,主线程一并退出。Timer里的get_t方法也退出
注意:ReqSender和Timer是两个线程类。
class MyError(Exception): print "a error"class ReqSender(): def __init__(self,ids,idb): self.ids=ids self.idb=idb def assert_case(self): if self.ids <> self.idb : try: raise MyError finally: exit(1)class Timer(): def get_t(self): for i in range(10): time.sleep(3) print iif __name__ == '__main__': rs = ReqSender(2,3) tm = Timer() thread.start_new_thread(tm.get_t,()) time.sleep(10) try: thread.start_new_thread(rs.assert_case,()) except MyError: print "exist" sys.exit(1)
但当代码编程assert 和get_t同属于一个线程类。当assert方法异常退出。get_t并不会退出,而是等get_t跑完,主线程才退出。。
class MyError(Exception): print "a error"class ReqSender(): def __init__(self,ids,idb): self.ids=ids self.idb=idb def assert_case(self): if self.ids <> self.idb : try: raise MyError finally: exit(1) def get_t(self): for i in range(10): time.sleep(3) print iif __name__ == '__main__': rs = ReqSender(2,3) thread.start_new_thread(rm.get_t,()) time.sleep(10) try: thread.start_new_thread(rs.assert_case,()) except MyError: print "exist" sys.exit(1)
这可能是一个线程,进程退出的问题。因为我对这块不太熟悉,所以mark以下,以后回来再思考深层原因。