import threading
import time
def foo1():
try:
1/0
finally:
print('foo1 fin')
def foo2():
time.sleep(4)
try:
foo1()
finally:
print('foo2 fin')
while True:
time.sleep(1)
t = threading.Thread(target=foo2)
t.start()#子线程
while True:#主线程
time.sleep(1)
print('-------------')
if t.is_alive():
print('alive')
else:
print('dead')
运行结果:
-------------
alive
-------------
alive
-------------
alive
foo1 fin
foo2 fin
-------------
alive
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/home/yzx/PycharmProjects/python/t32.py", line 14, in foo2
foo1()
File "/home/yzx/