Hello!

during shutdown AIO prints useful info about "forgotten" tasks..
but. the time when it is doing that is too late, and all modules get 
unloaded 
so it is hard to build good trace info. 

here is an example which shows the issue:

import asyncio
import inspect
import logging
import sys

class ExceptionFormatter(logging.Formatter):
    def formatException(self, exc_info):
        print('---->>> MODULES during shutdown %d'  % len(sys.modules))
        frame = inspect.currentframe()
        fi = inspect.getframeinfo(frame)
        return super().formatException(exc_info)

fh = logging.StreamHandler()
f = ExceptionFormatter('%(asctime)s|%(levelname)s|%(message)s|',
                              '%d/%m/%Y %H:%M:%S')
fh.setFormatter(f)
root = logging.getLogger()
root.setLevel(logging.DEBUG)
root.addHandler(fh)

print('MODULES during runtime %d' % len(sys.modules))

loop = asyncio.get_event_loop()
async def task():
    print('i am task.. press Ctrl+C to see the issue')
    raise RuntimeError('Oh Oh')

myself = loop.create_task(task())
loop.run_forever()



why is that a problem? -
because, for instance if i try to use "inspect"
and call it there - it will fail at line 721:

/home/http/Python-3.5.0/lib/python3.5/inspect.py

... 
if file in modulesbyfile:
    return sys.modules.get(modulesbyfile[file])
# Check the main module
 HERE>>>> main = sys.modules['__main__']
if not hasattr(object, '__name__'):
... 


it assumes that there is module "__main__", but it does not exist during 
shutdown!

please advice on how to approach the issue? 
what should i do to have good inspection and logging?
import asyncio
import inspect
import logging
import sys

class ExceptionFormatter(logging.Formatter):
    def formatException(self, exc_info):
        print('---->>> MODULES during shutdown %d'  % len(sys.modules))
        frame = inspect.currentframe()
        fi = inspect.getframeinfo(frame)
        return super().formatException(exc_info)

fh = logging.StreamHandler()
f = ExceptionFormatter('%(asctime)s|%(levelname)s|%(message)s|',
                              '%d/%m/%Y %H:%M:%S')
fh.setFormatter(f)
root = logging.getLogger()
root.setLevel(logging.DEBUG)
root.addHandler(fh)

print('MODULES during runtime %d' % len(sys.modules))

loop = asyncio.get_event_loop()
async def task():
    print('i am task.. press Ctrl+C to see the issue')
    raise RuntimeError('Oh Oh')

myself = loop.create_task(task())
loop.run_forever()

Reply via email to