2017-02-02 34 views
1

我正在写与multiproccesing Windows服务,我运行到与被称为池中的方法问题。的Python代码池没有运行

现在我能够安装服务,并运行它,它输出The service started running...日志文件,但没有别的。

看着进程资源管理器(见下面的屏幕截图),我看到进程正在创建并不断完成,但TestMethod内的代码没有运行,服务也没有退出池,因为没有任何东西其他人正在写入文件。

我不能停止该服务,因为它停留在游泳池,没有达到停止事件的检查。

为什么TestMethod的中的代码不运行呢?

Process Explorer Screenshot

服务代码:

import servicemanager 
import win32event 
import win32service 
import win32serviceutil 
import multiprocessing 


class TestService(win32serviceutil.ServiceFramework): 
    _svc_name_ = "TestService" 
    _svc_display_name_ = "Test Service" 

    def testMethod(self, testVar): 

     with open('C:\\Test.log', 'a') as f: 
      f.write('The method is running: ' + testVar) 
      f.close() 

    def __init__(self, args): 

     win32serviceutil.ServiceFramework.__init__(self, args) 
     self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) 
     socket.setdefaulttimeout(60) 

    def SvcStop(self): 

     self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
     win32event.SetEvent(self.hWaitStop) 

    def SvcDoRun(self): 

     with open('C:\\Test.log', 'a') as f: 
      f.write('The service started running...\n') 
      f.close() 

     rc = None 

     p = multiprocessing.Pool(5) 

     p.map(TestService.testMethod, range(1,6)) 

     with open('C:\\Test.log', 'a') as f: 
      f.write('Finished method...\n') 
      f.close() 

     while rc != win32event.WAIT_OBJECT_0:     
      with open('C:\\Test.log', 'a') as f: 
       f.write('The service is running...\n') 
       f.close() 
      rc = win32event.WaitForSingleObject(self.hWaitStop, 5000) 

     with open('C:\\Test.log', 'a') as f: 
       f.write('StreamCapture service stopped.\n') 
       f.close() 



if __name__ == '__main__': 
    if len(sys.argv) == 1: 
     servicemanager.Initialize() 
     servicemanager.PrepareToHostSingle(TestService) 
     servicemanager.StartServiceCtrlDispatcher() 
    else: 
     win32serviceutil.HandleCommandLine(TestService) 
+0

'p.map(TestService.testMethod,范围(1,6))' - 'TestService.testMethod'是一个未绑定的方法。在这个'p.map'调用中没有'TestService'的实例。你期望执行这种方法的对象是什么? – user2357112

+0

原来的方法传递视频流和它运行的ffmpeg,并创建日志。我把这个方法放在类里面,因为我认为它可能会遇到一个范围问题,但它仍然遇到了同样的问题,这个方法可以非常好地在类之外定义而没有自己,它会遇到与现在相同的问题。 –

回答

0

有两个问题在你的代码:

  • 你点mapTestService.testMethod,这是一个 “未绑定功能” 住在类的命名空间,但testMethod定义像一个类的方法。你要么需要self.testMethod调用它,或者从函数定义
  • 您尝试为int添加到字符串中删除的self,使用f.write('The method is running: {}'.format(testVar))代替

你的精简程序,修正应该是这样的:

import multiprocessing 

class TestService: 
    def testMethod(self, testVar): 
     with open('C:\\Test.log', 'a') as f: 
      f.write('The method is running: {}'.format(testVar)) 
      f.close() 

    def SvcDoRun(self): 
     p = multiprocessing.Pool(5) 
     p.map(self.testMethod, range(1,6)) 


if __name__ == "__main__": 
    d = TestService() 
    d.SvcDoRun() 

PS尝试在下次发布最小代码示例:将代码剥离至最小值,从而生成错误。我发布的代码片段足以解释问题。通过这种方式,读者更容易理解,并且您可以更快地获得答案。

0

该问题是由Windows上的pyinstaller和onefile可执行文件的已知问题引起的。

添加以下try块后,我的固定的进口对我来说:

try: 
    # Python 3.4+ 
    if sys.platform.startswith('win'): 
     import multiprocessing.popen_spawn_win32 as forking 
    else: 
     import multiprocessing.popen_fork as forking 
except ImportError: 
    import multiprocessing.forking as forking 

详情请参阅https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing