亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 4381 | 回復(fù): 2
打印 上一主題 下一主題

【已解決】win服務(wù)模式下,__import__的問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2013-09-11 19:34 |只看該作者 |倒序?yàn)g覽
本帖最后由 wfnh 于 2013-09-13 09:39 編輯

有2個(gè)腳本,一個(gè)是服務(wù)框架,一個(gè)是邏輯腳本,調(diào)試邏輯腳本或者是直接運(yùn)行邏輯腳本是正常的,__import__也沒問題,但如果是用服務(wù)的形式啟動(dòng),就import不了···很奇怪,求破,代碼如下:
  1. import win32serviceutil
  2. import win32service
  3. import win32event
  4. import time

  5. import EventDispatch
  6. from common.util import get_logger

  7. class MonitorService(win32serviceutil.ServiceFramework):
  8.     """
  9.     Usage: 'PythonService.py [options] install|update|remove|start [...]|stop|restart [...]|debug [...]'
  10.     Options for 'install' and 'update' commands only:
  11.      --username domain\username : The Username the service is to run under
  12.      --password password : The password for the username
  13.      --startup [manual|auto|disabled|delayed] : How the service starts, default = manual
  14.      --interactive : Allow the service to interact with the desktop.
  15.      --perfmonini file: .ini file to use for registering performance monitor data
  16.      --perfmondll file: .dll file to use when querying the service for
  17.        performance data, default = perfmondata.dll
  18.     Options for 'start' and 'stop' commands only:
  19.      --wait seconds: Wait for the service to actually start or stop.
  20.                      If you specify --wait with the 'stop' option, the service
  21.                      and all dependent services will be stopped, each waiting
  22.                      the specified period.
  23.     """
  24.     #服務(wù)名
  25.     _svc_name_ = "MonitorService"
  26.     #服務(wù)顯示名稱
  27.     _svc_display_name_ = "MonitorService"
  28.     #服務(wù)描述
  29.     _svc_description_ = u"監(jiān)控服務(wù)"

  30.     def __init__(self, args):
  31.         win32serviceutil.ServiceFramework.__init__(self, args)
  32.         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  33.         self.logger = get_logger('service')
  34.         self.manager = EventDispatch.EventDispatch()
  35.         self.isAlive = True

  36.     def SvcDoRun(self):
  37.         self.logger.info("MonitorService running....")
  38.         self.manager.add_all()
  39.         # 等待服務(wù)被停止
  40.         win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

  41.     def SvcStop(self):
  42.         # 先告訴SCM停止這個(gè)過程
  43.         self.logger.info("MonitorService stopped....")
  44.         self.manager.stop()
  45.         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  46.         # 設(shè)置事件
  47.         win32event.SetEvent(self.hWaitStop)
  48.         self.isAlive = False

  49. if __name__=='__main__':
  50.     win32serviceutil.HandleCommandLine(MonitorService)
復(fù)制代碼
add_all函數(shù):
  1.     def add_all(self):
  2.         import inspect
  3.         logger = get_logger('EventDispatch')
  4.         #--獲得當(dāng)前文件和當(dāng)前文件所在的目錄
  5.         this_file = inspect.getfile(inspect.currentframe())
  6.         this_dir = os.path.abspath(os.path.dirname(this_file))
  7.         path = os.path.join(this_dir,'customEvent')
  8.         class_list = import_all_module(path,'event.customEvent','evt_*.py')
  9.         if class_list:
  10.             self.event_queue = class_list
  11.         logger.info('EventDispatch_add_all end')
復(fù)制代碼
import_all_module函數(shù)(問題就在這個(gè)函數(shù)里邊):
  1. def import_all_module(path,import_from,pattern = '*.py'):
  2.     '''
  3.     導(dǎo)入路徑下的所有模塊,返回模塊列表,pattern參數(shù)可過濾不需要的模塊
  4.     '''
  5.     import_logger = get_logger('import')
  6.     if os.path.isdir(path) and os.path.exists(path):
  7.         pattern = os.path.join(path,pattern)
  8.     else:
  9.         return []
  10.     mod_name = []
  11.     class_objs = []
  12.     import_logger.info(pattern)
  13.     for module_file in glob.glob(pattern):
  14.         import_logger.info(module_file)
  15.         module_name,ext = os.path.splitext(os.path.basename(module_file))
  16.         mod_name.append(module_name)
  17.     try:
  18.         modules = __import__(import_from,globals(),locals(),mod_name)  #--問題就在這里
  19.         import_logger.info('------------------')  #--上面出問題后,這里不會(huì)走到,并且也不會(huì)觸發(fā)異常···不是很明白
  20.     except:
  21.         import_logger.info('ImportError,mod_name is ',mod_name)
  22.         print 'ImportError,mod_name is ',mod_name
  23.     import_logger.info('mod_name:%s'%(mod_name))
  24.     for mod in mod_name:
  25.         import_logger.info('for mod:%s'%(mod))
  26.         obj = getattr(modules,mod)
  27.         #--循環(huán)取到類對(duì)象為止
  28.         while(obj):
  29.             if isinstance(obj,types.ModuleType):   #--檢查是否為模塊類型
  30.                 obj = getattr(obj,mod)
  31.                 import_logger.info('while,continue:%s'%(obj))
  32.                 continue
  33.             elif isinstance(obj,types.TypeType): #--如果是類對(duì)象的話,就是目標(biāo)了
  34.                 class_objs.append(obj)
  35.                 import_logger.info('found class:%s'%(obj))
  36.                 break
  37.     return class_objs
復(fù)制代碼
如果是單獨(dú)運(yùn)行EventDispatch.EventDispatch().add_all()是沒問題的····看了一下log····導(dǎo)入的路徑也算正!ぁぁで笃

附上報(bào)錯(cuò)的信息:
  1.   File "D:\test-area\getItem\ghs\sf\monitorService\event\eventDispatch.py", line 43, in add_all
  2.     class_list = import_all_module(path,'event.customEvent','evt_*.py')
  3.   File "D:\test-area\getItem\ghs\sf\monitorService\common\util.py", line 51, in import_all_module
  4.     obj = getattr(modules,mod)
  5. UnboundLocalError: local variable 'modules' referenced before assignment
復(fù)制代碼

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2013-09-13 09:41 |只看該作者
服務(wù)模式下··和純腳本的形式,sys.path的路徑是不同的,所以有些第三方庫沒include進(jìn)來,導(dǎo)致內(nèi)部的一些依賴不全,import失敗,但我奇怪的是except:  這樣居然捕獲不了錯(cuò)誤,要except Exception,e:才可以

論壇徽章:
2
酉雞
日期:2014-02-19 09:11:08摩羯座
日期:2014-05-23 10:16:16
3 [報(bào)告]
發(fā)表于 2013-09-13 10:02 |只看該作者
所以不能偷懶啊,要用正規(guī)except Exception的寫法啊:wink: 回復(fù) 2# wfnh


   
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP