简体   繁体   中英

Why a python method not returning?

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?

您是否在查看同一记录器?

Is your log level above to debug? Try with print

Also, try adding a log before the call.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM