我正在写与multiproccesing Windows服务,我运行到与被称为池中的方法问题。的Python代码池没有运行
现在我能够安装服务,并运行它,它输出The service started running...
日志文件,但没有别的。
看着进程资源管理器(见下面的屏幕截图),我看到进程正在创建并不断完成,但TestMethod内的代码没有运行,服务也没有退出池,因为没有任何东西其他人正在写入文件。
我不能停止该服务,因为它停留在游泳池,没有达到停止事件的检查。
为什么TestMethod的中的代码不运行呢?
服务代码:
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)
'p.map(TestService.testMethod,范围(1,6))' - 'TestService.testMethod'是一个未绑定的方法。在这个'p.map'调用中没有'TestService'的实例。你期望执行这种方法的对象是什么? – user2357112
原来的方法传递视频流和它运行的ffmpeg,并创建日志。我把这个方法放在类里面,因为我认为它可能会遇到一个范围问题,但它仍然遇到了同样的问题,这个方法可以非常好地在类之外定义而没有自己,它会遇到与现在相同的问题。 –