mhammond / pywin32

Python for Windows (pywin32) Extensions
4.9k stars 783 forks source link

pythonservice.exe can't be updated if a python service is already running #2194

Open zcc0077 opened 4 months ago

zcc0077 commented 4 months ago

Note that issues in this repository are only for bugs or feature requests in the pywin32.

There is a problem, I need to install multiple python script services, I installed the first one is fine, the output is:

Installing service Service1 
copying host exe 'C:\Python311\Lib\site-packages\win32\pythonservice.exe' -> 'C:\Python311\pythonservice.exe' 
Service installed 

When I install another service, I get an error:

Installing service Service2 
copying host exe 'C:\Python311\Lib\site-packages\win32\pythonservice.exe' -> 'C:\Python311\pythonservice.exe' 
Error installing service: The process cannot access the file because it is being used by another process. (32) 

I found that because C:\Python311\pythonservice.exe is occupied by the first service, it cannot be copied successfully. In this case, I can only stop the previous service and then install other services, which will make my service deployment very troublesome (I did not have this problem when using python2 before).

mhammond commented 4 months ago

The reason this is done is because you might have upgraded pywin32 and trying to use a new pywin32 with an old .exe is going to end badly. An option would be to add a command-line param to the service install code to skip this step, which I'd be happy to take a PR for.

zcc0077 commented 4 months ago

Thanks a lot :), I fixed temporarily by explicitly assign exe to _exe_name_ and no copy action was executed:

class MySrv(win32serviceutil.ServiceFramework):   
    _svc_name_ = "MySrv"  
    _svc_display_name_ = "My Service"
    _exe_name_ = "C:/Python311/Lib/site-packages/win32/pythonservice.exe"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)