我的主脚本中有一段代码,用于从数组启动线程。我在python脚本中为不同的线程编写了不同的代码。有一个特别的代码,我不明白为什么当我遇到一个异常,它似乎挂起整个程序/脚本我有
import threading #(of course)
def start_threads(threads)
for t in threads:
t.start()
for t in threads:
t.join()
return
threads = []
thread1 = threading.Thread(target=start_test1,args=(self,))
thread1.daemon = True
threads.append(thread1)
thread2 = threading.Thread(target=start_test2,args=(self,))
thread2.daemon = True
threads.append(thread2)
start_threads(threads)
因此,thread1的代码在遇到任何异常时都不会导致主程序/脚本停止。由于某些原因,thread2中的代码不起作用。我正在为两个线程的代码中执行try/except,所以我不知道为什么thread2不能正确返回
thread2内部的代码类似于:
try:
print('starting thread2')
some_function(options, parameters, etc)
print('thread2 complete')
except Exception as e:
print('error running thread2')
print('msg: %s' % e)
我甚至在异常中的最后一个打印行之后添加了一个“return”,当我在中的最后一个打印行之后调试时,或者如果我在那里放置了一个return,则什么都不会发生,这意味着遍历这些行似乎没问题,但它永远不会返回到我的主代码以退出程序/脚本。我在控制台上看到了打印消息,但我的代码应该会带着另一条消息退出主程序
目前没有回答
相关问题 更多 >
编程相关推荐