私はマルチプロセッシングを使用してWindowsサービスを作成していますが、プールで呼び出されたメソッドに問題があります。Pythonプールコードが実行されていません
サービスをインストールして実行すると、The service started running...
がログファイルに出力されますが、それ以外は何も出力されません。
プロセスエクスプローラ(下のスクリーンショットを参照)を見ると、プロセスが作成されて絶えず終了しているのがわかりますが、TestMethod内のコードは実行されておらず、サービスはプールを終了しませんelseがファイルに書き込まれています。
サービスがプールに滞留し、停止イベントのチェックに達しないため、サービスを停止できません。
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))'のためhttps://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessingを参照してください - 'TestService.testMethod'はバインドされていないメソッドです。この 'p.map'呼び出しには' TestService'のインスタンスはありません。このメソッドを実行するには、どのオブジェクトが必要ですか? – user2357112
元々、このメソッドはビデオストリームに渡され、ffmpegを実行してログを作成します。私はそれがスコープの問題に遭遇する可能性があると思ったので、クラスの中にメソッドを入れましたが、それはまだ同じ問題に走っています。メソッドは自分自身なしでクラスの外で定義でき、 。 –