为什么 python 方法不返回?

发布于 2024-10-06 13:54:09 字数 918 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终弃我 2024-10-13 13:54:09

你在看同一个记录器吗?

Are you looking at the same logger?

恰似旧人归 2024-10-13 13:54:09

您的日志级别是否高于调试级别?尝试使用 print

另外,尝试在调用之前添加日志。

Is your log level above to debug? Try with print

Also, try adding a log before the call.

路还长,别太狂 2024-10-13 13:54:09

我认为我们需要比这更多的信息 - 可能是关于“做某事”和 input_string 的上下文

我将其放入一段代码中:

import logging                                                                                          
import time                                                                                             

logger = logging.getLogger('')                                                                          

class device_instance(object):                                                                          

    def destroy(self):                                                                                  
        self.logger.warning('Instance is being destroyed')                                              
        # do stuff                                                                                      
        self.logger.warning('Instance is destroyed')                                                    

input_strings = ['one', 'two', 'destroy']                                                               

if (__name__ == '__main__'):                                                                            
    logging.basicConfig()                                                                               
    try:                                                                                                
        instance = device_instance()                                                                    
        instance.logger = logger                                                                        

        gen = input_strings.__iter__()                                                                  

        while True:                                                                                     
            #do stuff                                                                                   
            try:                                                                                        
                input_string = gen.next()                                                               
            except StopIteration:                                                                       
                input_string = 'destroy'                                                                

            if input_string == 'destroy':                                                               
                instance.destroy()                                                                      
                logger.warning("Back in main after destroying")                                         
                break                                                                                   
            else:                                                                                       
                time.sleep(.100)                                                                        
    except:                                                                                             
        logger.exception("Unhandled exception occurred")

这会输出您所期望的内容:

WARNING:root:Instance is being destroyed                                                                    
WARNING:root:Instance is destroyed                                                                          
WARNING:root:Back in main after destroying

所以我认为还有其他因素在起作用。

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:

import logging                                                                                          
import time                                                                                             

logger = logging.getLogger('')                                                                          

class device_instance(object):                                                                          

    def destroy(self):                                                                                  
        self.logger.warning('Instance is being destroyed')                                              
        # do stuff                                                                                      
        self.logger.warning('Instance is destroyed')                                                    

input_strings = ['one', 'two', 'destroy']                                                               

if (__name__ == '__main__'):                                                                            
    logging.basicConfig()                                                                               
    try:                                                                                                
        instance = device_instance()                                                                    
        instance.logger = logger                                                                        

        gen = input_strings.__iter__()                                                                  

        while True:                                                                                     
            #do stuff                                                                                   
            try:                                                                                        
                input_string = gen.next()                                                               
            except StopIteration:                                                                       
                input_string = 'destroy'                                                                

            if input_string == 'destroy':                                                               
                instance.destroy()                                                                      
                logger.warning("Back in main after destroying")                                         
                break                                                                                   
            else:                                                                                       
                time.sleep(.100)                                                                        
    except:                                                                                             
        logger.exception("Unhandled exception occurred")

This outputs what you'd expect:

WARNING:root:Instance is being destroyed                                                                    
WARNING:root:Instance is destroyed                                                                          
WARNING:root:Back in main after destroying

So something else is at play, I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文