- 论坛徽章:
- 0
|
本帖最后由 wfnh 于 2013-09-13 09:39 编辑
有2个脚本,一个是服务框架,一个是逻辑脚本,调试逻辑脚本或者是直接运行逻辑脚本是正常的,__import__也没问题,但如果是用服务的形式启动,就import不了···很奇怪,求破,代码如下:- import win32serviceutil
- import win32service
- import win32event
- import time
- import EventDispatch
- from common.util import get_logger
- class MonitorService(win32serviceutil.ServiceFramework):
- """
- Usage: 'PythonService.py [options] install|update|remove|start [...]|stop|restart [...]|debug [...]'
- Options for 'install' and 'update' commands only:
- --username domain\username : The Username the service is to run under
- --password password : The password for the username
- --startup [manual|auto|disabled|delayed] : How the service starts, default = manual
- --interactive : Allow the service to interact with the desktop.
- --perfmonini file: .ini file to use for registering performance monitor data
- --perfmondll file: .dll file to use when querying the service for
- performance data, default = perfmondata.dll
- Options for 'start' and 'stop' commands only:
- --wait seconds: Wait for the service to actually start or stop.
- If you specify --wait with the 'stop' option, the service
- and all dependent services will be stopped, each waiting
- the specified period.
- """
- #服务名
- _svc_name_ = "MonitorService"
- #服务显示名称
- _svc_display_name_ = "MonitorService"
- #服务描述
- _svc_description_ = u"监控服务"
- def __init__(self, args):
- win32serviceutil.ServiceFramework.__init__(self, args)
- self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
- self.logger = get_logger('service')
- self.manager = EventDispatch.EventDispatch()
- self.isAlive = True
- def SvcDoRun(self):
- self.logger.info("MonitorService running....")
- self.manager.add_all()
- # 等待服务被停止
- win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
- def SvcStop(self):
- # 先告诉SCM停止这个过程
- self.logger.info("MonitorService stopped....")
- self.manager.stop()
- self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
- # 设置事件
- win32event.SetEvent(self.hWaitStop)
- self.isAlive = False
- if __name__=='__main__':
- win32serviceutil.HandleCommandLine(MonitorService)
复制代码 add_all函数:- def add_all(self):
- import inspect
- logger = get_logger('EventDispatch')
- #--获得当前文件和当前文件所在的目录
- this_file = inspect.getfile(inspect.currentframe())
- this_dir = os.path.abspath(os.path.dirname(this_file))
- path = os.path.join(this_dir,'customEvent')
- class_list = import_all_module(path,'event.customEvent','evt_*.py')
- if class_list:
- self.event_queue = class_list
- logger.info('EventDispatch_add_all end')
复制代码 import_all_module函数(问题就在这个函数里边):- def import_all_module(path,import_from,pattern = '*.py'):
- '''
- 导入路径下的所有模块,返回模块列表,pattern参数可过滤不需要的模块
- '''
- import_logger = get_logger('import')
- if os.path.isdir(path) and os.path.exists(path):
- pattern = os.path.join(path,pattern)
- else:
- return []
- mod_name = []
- class_objs = []
- import_logger.info(pattern)
- for module_file in glob.glob(pattern):
- import_logger.info(module_file)
- module_name,ext = os.path.splitext(os.path.basename(module_file))
- mod_name.append(module_name)
- try:
- modules = __import__(import_from,globals(),locals(),mod_name) #--问题就在这里
- import_logger.info('------------------') #--上面出问题后,这里不会走到,并且也不会触发异常···不是很明白
- except:
- import_logger.info('ImportError,mod_name is ',mod_name)
- print 'ImportError,mod_name is ',mod_name
- import_logger.info('mod_name:%s'%(mod_name))
- for mod in mod_name:
- import_logger.info('for mod:%s'%(mod))
- obj = getattr(modules,mod)
- #--循环取到类对象为止
- while(obj):
- if isinstance(obj,types.ModuleType): #--检查是否为模块类型
- obj = getattr(obj,mod)
- import_logger.info('while,continue:%s'%(obj))
- continue
- elif isinstance(obj,types.TypeType): #--如果是类对象的话,就是目标了
- class_objs.append(obj)
- import_logger.info('found class:%s'%(obj))
- break
- return class_objs
复制代码 如果是单独运行EventDispatch.EventDispatch().add_all()是没问题的····看了一下log····导入的路径也算正常···求破
附上报错的信息:- File "D:\test-area\getItem\ghs\sf\monitorService\event\eventDispatch.py", line 43, in add_all
- class_list = import_all_module(path,'event.customEvent','evt_*.py')
- File "D:\test-area\getItem\ghs\sf\monitorService\common\util.py", line 51, in import_all_module
- obj = getattr(modules,mod)
- UnboundLocalError: local variable 'modules' referenced before assignment
复制代码 |
|