sys.modules是一个全局字典,该字典是python启动后就加载在内存中。每当程序员导入新的模块,sys.modules都将记录这些模块。字典sys.modules对于加载模块起到了缓冲的作用。当某个模块第一次导入,字典sys.modules将自动记录该模块。当第二次再导入该模块时,python会直接到字典中查找,从而加快了程序运行的速度。
字典sys.modules具有字典所拥有的一切方法,可以通过这些方法了解当前的环境加载了哪些模块
import sys
print sys.modules.keys()
print sys.modules.values()
print sys.modules["os"]
import sys
print(sys.modules[__name__])
print(sys.modules.values())
print(sys.modules.keys())
print(sys.modules.items())
import sys
__all__ = [
'TIMER_ITIMER_CALLBACK',
'BASESTUB_TIMER_CREATE_BASEAPPS',
'BASESTUB_TIMER_BASEAPPS',
'BASESTUB_TIMER_LOAD_ENTITY_DBIDS',
'BASESTUB_TIMER_PREPARE_CREATE_SPACES',
'BASESTUB_TIMER_CREATE_SPACES',
'BASESTUB_TIMER_SPACES_LOADING',
'BASESTUB_TIMER_SPACE_MARKER',
'BASESTUB_TIMER_CREATE_GLOBAL_STUBS',
'BASESTUB_TIMER_GLOBAL_STUBS_HALF_PREPARE',
'BASESTUB_TIMER_GLOBAL_STUBS_FULL_PREPARE',
'BASESTUB_TIMER_CREATE_LINE_SPACE_ENTITIES',
'BASESTUB_TIMER_CHECK_LINE_READY',
'BASESTUB_TIMER_GAME_READY',
'BASEAPP_TIMER_INIT',
'BASEAPP_TIMER_CREATE_SPACE_MARKER',
'TIMER_CELL_SAFE_DESTROY',
'TIMER_CELL_TTL_DESTROY',
'SPACE_MAP_STUB_READY',
'TIMER_TYPE_DESTROY',
'TIMER_SEMI_DESTROY',
'MONSTER_AI_THINK',
'SUMMON_AI_THINK',
'PET_AI_THINK',
'CNPC_AI_THINK',
'AVATARMIRROR_AI_THINK',
'SPACE_MGR_AI_THINK',
'SUMMON_CHECK_OWNER_VALID',
'HOME_CHECK_EMPTY_HOME',
'HOME_CHECK_DESTROY',
'HOME_STUB_CHECK_OLD_HOME',
'DUNGEON_CHECK_DESTROY',
'BTF_DUNGEON_CHECK_DESTROY',
'BTF_DUNGEON_MATCH_PLAYERS',
'BTF_DUNGEON_REFRESH_MATCH_CACHE',
'BTF_DUNGEON_REFRESH_OCCUPATION_PRG',
'DUNGEON_TIME_LINE_CHECK',
'HOME_ADVENTURE_TIME_LINE_CHECK',
'CREATION_LOOP',
'COMBAT_REGEN',
'AVATAR_SYNC_SERVER_TIME',
'SPACE_MAP_DATA_LOADING',
'ROUTE_NODE_MOVE',
'ADMIN_STUB_ASYNC_TICK',
'ASYNCORE_TICK',
'AUTO_COMBAT_CHECK',
'FOLLOW_CAPTAIN_CHECK',
'TEAM_TICK',
'FRIEND_TORNADO_TICK',
'AVATAR_UPDATE_AREAID',
'AVATAR_RED_NAME_CHECK',
'GUILD_CHECK_DESTROY',
'ACTIVITY_STAT_UPDATE'
]
TIMER_ID_GENERATOR = (i for i in range(1,1000000))
def defineTimer(timerName):
print("---__name__=[%s]" % __name__)
modSelf = sys.modules[__name__]
print("---modSelf={} timerName={}".format(modSelf, timerName))
setattr(modSelf, timerName, next(TIMER_ID_GENERATOR))
for name in __all__:
defineTimer(name)
================================================defineTimer==========================================
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=TIMER_ITIMER_CALLBACK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_CREATE_BASEAPPS
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_BASEAPPS
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_LOAD_ENTITY_DBIDS
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_PREPARE_CREATE_SPACES
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_CREATE_SPACES
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_SPACES_LOADING
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_SPACE_MARKER
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_CREATE_GLOBAL_STUBS
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_GLOBAL_STUBS_HALF_PREPARE
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_GLOBAL_STUBS_FULL_PREPARE
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_CREATE_LINE_SPACE_ENTITIES
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_CHECK_LINE_READY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASESTUB_TIMER_GAME_READY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASEAPP_TIMER_INIT
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BASEAPP_TIMER_CREATE_SPACE_MARKER
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=TIMER_CELL_SAFE_DESTROY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=TIMER_CELL_TTL_DESTROY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=SPACE_MAP_STUB_READY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=TIMER_TYPE_DESTROY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=TIMER_SEMI_DESTROY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=MONSTER_AI_THINK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=SUMMON_AI_THINK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=PET_AI_THINK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=CNPC_AI_THINK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=AVATARMIRROR_AI_THINK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=SPACE_MGR_AI_THINK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=SUMMON_CHECK_OWNER_VALID
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=HOME_CHECK_EMPTY_HOME
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=HOME_CHECK_DESTROY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=HOME_STUB_CHECK_OLD_HOME
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=DUNGEON_CHECK_DESTROY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BTF_DUNGEON_CHECK_DESTROY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BTF_DUNGEON_MATCH_PLAYERS
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BTF_DUNGEON_REFRESH_MATCH_CACHE
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=BTF_DUNGEON_REFRESH_OCCUPATION_PRG
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=DUNGEON_TIME_LINE_CHECK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=HOME_ADVENTURE_TIME_LINE_CHECK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=CREATION_LOOP
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=COMBAT_REGEN
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=AVATAR_SYNC_SERVER_TIME
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=SPACE_MAP_DATA_LOADING
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=ROUTE_NODE_MOVE
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=ADMIN_STUB_ASYNC_TICK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=ASYNCORE_TICK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=AUTO_COMBAT_CHECK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=FOLLOW_CAPTAIN_CHECK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=TEAM_TICK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=FRIEND_TORNADO_TICK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=AVATAR_UPDATE_AREAID
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=AVATAR_RED_NAME_CHECK
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=GUILD_CHECK_DESTROY
---__name__=[__main__]
---modSelf=<module '__main__' from 'F:/python-test/decorator/myDecorator.py'> timerName=ACTIVITY_STAT_UPDATE