1

I am using python 2.6, pywin32 build 217 and Windows 7.

I created a windows services as follows:

class Service(win32serviceutil.ServiceFramework):

    _svc_name_ = 'MPTESTER'
    _svc_display_name_ = 'MP TESTER'
    _svc_description_ = "NA"
    _scratch_workspace_ = os.environ["TEMP"]
    _process_count_ = (int(os.environ["NUMBER_OF_PROCESSORS"]) *2) -1
    _pool_ = None
    def __init__(self, *args):
        win32serviceutil.ServiceFramework.__init__(self, *args)
        self.log('init')
        self.runFlag = True
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)
    def log(self, msg):
        import servicemanager
        servicemanager.LogInfoMsg(str(msg))
    def sleep(self, sec):
        win32api.Sleep(sec*1000, True)
    def SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
        try:
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
            self.log('start')
            self.start()
            while self.runflag == True:
                pass
            self.log('wait')
            win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)
            self.log('done')
        except Exception, x:
            self.log('Exception : %s' % x)
            self.SvcStop()
    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.log('stopping')
        self.stop()
        self.log('stopped')
        win32event.SetEvent(self.stop_event)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)
    def start(self):
        dummyFilePath = r"c:\temp\errorLog.log"
        with open(dummyFilePath,'w') as dummy:
            #pythonFile = os.path.basename(str(inspect.getfile(inspect.currentframe())))
            #scriptPath = str(inspect.getfile(inspect.currentframe())).replace(os.sep + pythonFile,"")
            dummy.write('test 1\n')
            dummy.flush()
            pythonExe = os.path.join(sys.exec_prefix, 'python.exe')
            multiprocessing.set_executable(pythonExe)
            dummy.write('test 2\n')
            dummy.flush()
            if self.runFlag == None:
                self.runFlag = True
            dummy.write('test 3\n')
            dummy.flush()
            while self.runFlag:
                dummy.write('test 4\n')
                dummy.flush()
                results = []
                pool = multiprocessing.Pool((int(os.environ["NUMBER_OF_PROCESSORS"]) *2) -1)
                dummy.write("After POOL CREATED")
                dummy.flush()
                for i in range(self._process_count_):
                    dummy.write('test in range \n')
                    dummy.flush()
                    results.append(pool.apply_async(someLongFunction, 
                                                   [r"c:\temp",
                                                    "test_" + str(i) + ".txt"
                                                         ] ))

                #    Wait for all processes to finish
                #
                pool.close()
                pool.join()
                dummy.write("WAITING TO FINISH!")
                dummy.flush()
                #    delete the references
                #
                del results
                del pool
                dummy.write('fin test \n')
                dummy.flush()
                self.stop()
                break
    def stop(self): 
        self.runFlag = False

My issue is that the multiprocessing instances never fire. Is there a way to get the multiprocessing module to work? I could use subprocessing, but I really don't want to have to maintain two python files.

Thanks

1
  • by the way you should do all the processing in other module, it's a litle messy ;) Commented Apr 26, 2012 at 19:05

1 Answer 1

1

In fact python has a bug on the ..multiprocessing/forking.py module the reason is this:

When the program is running as a Windows service, but is not packaged into a single executable, main_path will become the path to the service executable (typically, pythonservice.exe). When this data makes it to the child process, the prepare() function will treat main_path as a path to a python module, and will try to import it. This causes it to fail.

you can find the patch here
or download the whole file from here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.