为什么 python 方法不返回?
我有一个 destroy() 方法,它在我的线程关闭之前被调用。
def destroy(self):
self.logger.debug("Instance is being destroyed")
//do stuff
self.logger.debug("Instance has been destroyed")
这是调用它的代码:
if (__name__ == '__main__'):
try:
instance = device_instance()
while True:
//do stuff
if input_string == 'destroy':
instance.destroy()
logger.debug("Back in main after destroying")
break
else:
//do stuff
time.sleep(.100)
except Exception, ex:
logger.debug("Exception occurred" + str(ex))
except:
logger.debug("Unhandled exception occurred")
现在,当我运行它时,问题是我看到日志语句“实例正在被销毁”和“实例已被销毁”,而我没有看到“销毁后返回主程序”。这意味着我的 destroy() 永远不会返回。我尝试添加显式返回语句,仍然是同样的问题。如果我在末尾添加 sys.exit(0) 到 destroy() ,它确实会引发异常,最终在 main 中捕获。 可能是什么问题?
I have a destroy() method which is called before my thread is coming down.
def destroy(self):
self.logger.debug("Instance is being destroyed")
//do stuff
self.logger.debug("Instance has been destroyed")
This is the code from where it is called:
if (__name__ == '__main__'):
try:
instance = device_instance()
while True:
//do stuff
if input_string == 'destroy':
instance.destroy()
logger.debug("Back in main after destroying")
break
else:
//do stuff
time.sleep(.100)
except Exception, ex:
logger.debug("Exception occurred" + str(ex))
except:
logger.debug("Unhandled exception occurred")
Now when I run it, the problem is I see logging statements "Instance is being destroyed" and "Instance has been destroyed" and I don't see "Back in main after destroying". That means my destroy() is never returning. I tried adding explicit return statement, still the same problem. If I add sys.exit(0) to destroy() at the end, it does raise exception which is eventually caught in main.
What could be the issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在看同一个记录器吗?
Are you looking at the same logger?
您的日志级别是否高于调试级别?尝试使用
print
另外,尝试在调用之前添加日志。
Is your log level above to debug? Try with
print
Also, try adding a log before the call.
我认为我们需要比这更多的信息 - 可能是关于“做某事”和
input_string
的上下文我将其放入一段代码中:
这会输出您所期望的内容:
所以我认为还有其他因素在起作用。
I think we'll need a bit more information than this -- probably about the context of 'do something' and
input_string
I put this into a piece of code:
This outputs what you'd expect:
So something else is at play, I think.