FAQ
Hi All,

I am attempting to create a Windows Service in Python.

I have the framework (from Mark Hammond and Andy Robinason's book)
running - see below. It starts fine - but it will not stop. :(

net stop "Python Service"

and using the services GUI both leave the services showing it as "stopping"

I guess this means SvcStop is called but it is not enough to get it out
of the machine.

Does anyone know why not?

Python 2.7 with win32 extensions, sunning on Windows 7.

Many thanks

Ian

the (complete) source code is
#!/usr/bin/env python
# coding=utf8
# service.py = testing services and Named pipes
#
import win32serviceutil
import win32service
import win32event
class PythonService(win32serviceutil.ServiceFramework):
_svc_name_ = "Python Service"
_svc_display_name_ = "Test Service in Python"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
wind32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PythonService)

Search Discussions

  • David at Sep 2, 2010 at 5:03 pm

    Il Thu, 02 Sep 2010 16:22:04 +0100, Ian Hobson ha scritto:

    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    You may try to give a WaitHint parameter to ReportServiceStatus call,
    otherwise the Service Manager will expect the service is stopped istantly.

    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 1000)


    David
  • Ian at Sep 2, 2010 at 6:45 pm

    On 02/09/2010 18:03, David wrote:
    Il Thu, 02 Sep 2010 16:22:04 +0100, Ian Hobson ha scritto:
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    You may try to give a WaitHint parameter to ReportServiceStatus call,
    otherwise the Service Manager will expect the service is stopped istantly.

    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 1000)


    David
    Thanks David.

    Sadly that makes no difference.
  • Ipatrol6010 at Sep 2, 2010 at 6:13 pm
    Well for one, if you're writing with pywin32, you certainly don't need the shbang line. #! /usr/bin/env is purely a POSIX thing.
  • Edward Kozlowski at Sep 2, 2010 at 7:06 pm

    On Sep 2, 10:22?am, Ian Hobson wrote:
    Hi All,

    I am attempting to create a Windows Service in Python.

    I have the framework (from Mark Hammond and Andy Robinason's book)
    running - see below. It starts fine - but it will not stop. :(

    net stop "Python Service"

    and using the services GUI both leave the services showing it as "stopping"

    I guess this means SvcStop is called but it is not enough to get it out
    of the machine.

    Does anyone know why not?

    Python 2.7 with win32 extensions, sunning on Windows 7.

    Many thanks

    Ian

    the (complete) source code is
    #!/usr/bin/env python
    # coding=utf8
    # ? service.py ?= testing services and Named pipes
    #
    import win32serviceutil
    import win32service
    import win32event
    class PythonService(win32serviceutil.ServiceFramework):
    ? ?_svc_name_ = "Python Service"
    ? ?_svc_display_name_ = "Test Service in Python"
    ? ?def __init__(self, args):
    ? ? ?win32serviceutil.ServiceFramework.__init__(self,args)
    ? ? ?self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    ? ?def SvcStop(self):
    ? ? ?self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    ? ? ?wind32event.SetEvent(self.hWaitStop)
    ? ?def SvcDoRun(self):
    ? ? ?win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)
    if __name__ == '__main__':
    ? ?win32serviceutil.HandleCommandLine(PythonService)
    Looks to me like there may be a typo in your code.

    You probably meant win32event.SetEvent(self.hWaitStop), not
    wind32event.

    Regards,
    -Edward Kozlowski
  • Ian at Sep 2, 2010 at 7:38 pm

    On 02/09/2010 20:06, Edward Kozlowski wrote:
    On Sep 2, 10:22 am, Ian Hobsonwrote:
    Hi All,

    I am attempting to create a Windows Service in Python.

    I have the framework (from Mark Hammond and Andy Robinason's book)
    running - see below. It starts fine - but it will not stop. :(

    net stop "Python Service"

    and using the services GUI both leave the services showing it as "stopping"

    I guess this means SvcStop is called but it is not enough to get it out
    of the machine.

    Does anyone know why not?

    Python 2.7 with win32 extensions, sunning on Windows 7.

    Many thanks

    Ian

    the (complete) source code is
    #!/usr/bin/env python
    # coding=utf8
    # service.py = testing services and Named pipes
    #
    import win32serviceutil
    import win32service
    import win32event
    class PythonService(win32serviceutil.ServiceFramework):
    _svc_name_ = "Python Service"
    _svc_display_name_ = "Test Service in Python"
    def __init__(self, args):
    win32serviceutil.ServiceFramework.__init__(self,args)
    self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    def SvcStop(self):
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    wind32event.SetEvent(self.hWaitStop)
    def SvcDoRun(self):
    win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)
    if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(PythonService)
    Looks to me like there may be a typo in your code.

    You probably meant win32event.SetEvent(self.hWaitStop), not
    wind32event.

    Regards,
    -Edward Kozlowski
    A huge big thank you Edward. That was the problem.

    Regards

    Ian
  • Edward Kozlowski at Sep 2, 2010 at 7:55 pm

    On Sep 2, 2:38?pm, Ian wrote:
    ? On 02/09/2010 20:06, Edward Kozlowski wrote:


    On Sep 2, 10:22 am, Ian Hobson<i... at ianhobson.co.uk> ?wrote:
    Hi All,
    I am attempting to create a Windows Service in Python.
    I have the framework (from Mark Hammond and Andy Robinason's book)
    running - see below. It starts fine - but it will not stop. :(
    net stop "Python Service"
    and using the services GUI both leave the services showing it as "stopping"
    I guess this means SvcStop is called but it is not enough to get it out
    of the machine.
    Does anyone know why not?
    Python 2.7 with win32 extensions, sunning on Windows 7.
    Many thanks
    Ian
    the (complete) source code is
    #!/usr/bin/env python
    # coding=utf8
    # ? service.py ?= testing services and Named pipes
    #
    import win32serviceutil
    import win32service
    import win32event
    class PythonService(win32serviceutil.ServiceFramework):
    ? ? _svc_name_ = "Python Service"
    ? ? _svc_display_name_ = "Test Service in Python"
    ? ? def __init__(self, args):
    ? ? ? win32serviceutil.ServiceFramework.__init__(self,args)
    ? ? ? self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    ? ? def SvcStop(self):
    ? ? ? self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    ? ? ? wind32event.SetEvent(self.hWaitStop)
    ? ? def SvcDoRun(self):
    ? ? ? win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)
    if __name__ == '__main__':
    ? ? win32serviceutil.HandleCommandLine(PythonService)
    Looks to me like there may be a typo in your code.
    You probably meant win32event.SetEvent(self.hWaitStop), not
    wind32event.
    Regards,
    -Edward Kozlowski
    A huge big thank you Edward. ?That was the problem.

    Regards

    Ian
    You're most welcome.

    If you're looking at running services in Windows using Python, one
    other hangup I ran into was that my services would freeze for no
    reason. At Pycon '09, I learned that there were buffers for stdout
    and stderr that were filling. I wish I could remember who gave the
    talk that included the jewel of knowledge, because I'd love to give
    credit where it's due...

    After I redirected stdout and stderr to files, my problems with the
    services freezing went away.

    Regards,
    -Edward Kozlowski
  • Ian at Sep 3, 2010 at 9:31 pm

    On 02/09/2010 20:55, Edward Kozlowski wrote:
    On Sep 2, 2:38 pm, Ianwrote:
    On 02/09/2010 20:06, Edward Kozlowski wrote:


    On Sep 2, 10:22 am, Ian Hobsonwrote:
    Hi All,
    I am attempting to create a Windows Service in Python.
    I have the framework (from Mark Hammond and Andy Robinason's book)
    running - see below. It starts fine - but it will not stop. :(
    net stop "Python Service"
    and using the services GUI both leave the services showing it as "stopping"
    I guess this means SvcStop is called but it is not enough to get it out
    of the machine.
    Does anyone know why not?
    Python 2.7 with win32 extensions, sunning on Windows 7.
    Many thanks
    Ian
    the (complete) source code is
    #!/usr/bin/env python
    # coding=utf8
    # service.py = testing services and Named pipes
    #
    import win32serviceutil
    import win32service
    import win32event
    class PythonService(win32serviceutil.ServiceFramework):
    _svc_name_ = "Python Service"
    _svc_display_name_ = "Test Service in Python"
    def __init__(self, args):
    win32serviceutil.ServiceFramework.__init__(self,args)
    self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    def SvcStop(self):
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    wind32event.SetEvent(self.hWaitStop)
    def SvcDoRun(self):
    win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)
    if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(PythonService)
    Looks to me like there may be a typo in your code.
    You probably meant win32event.SetEvent(self.hWaitStop), not
    wind32event.
    Regards,
    -Edward Kozlowski
    A huge big thank you Edward. That was the problem.

    Regards

    Ian
    You're most welcome.

    If you're looking at running services in Windows using Python, one
    other hangup I ran into was that my services would freeze for no
    reason. At Pycon '09, I learned that there were buffers for stdout
    and stderr that were filling. I wish I could remember who gave the
    talk that included the jewel of knowledge, because I'd love to give
    credit where it's due...

    After I redirected stdout and stderr to files, my problems with the
    services freezing went away.

    Regards,
    -Edward Kozlowski
    Hi Edward,

    Thanks for the heads up. That is really worth knowing.

    Ian
  • Mark Hammond at Sep 3, 2010 at 12:38 am

    On 3/09/2010 1:22 AM, Ian Hobson wrote:
    Hi All,

    I am attempting to create a Windows Service in Python.

    I have the framework (from Mark Hammond and Andy Robinason's book)
    running - see below. It starts fine - but it will not stop. :(

    net stop "Python Service"

    and using the services GUI both leave the services showing it as "stopping"

    I guess this means SvcStop is called but it is not enough to get it out
    of the machine.

    Does anyone know why not?
    I expect that the Windows Event Log might have some clues, as would
    attempting to use it in "debug" mode.
    def SvcStop(self):
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    wind32event.SetEvent(self.hWaitStop)
    Note the typo in the line above...

    Mark
  • Ian at Sep 3, 2010 at 9:27 pm

    On 03/09/2010 01:38, Mark Hammond wrote:
    I expect that the Windows Event Log might have some clues, as would
    attempting to use it in "debug" mode.
    Thanks Mark.

    The error log holds the trackback - it identified the line with the typo.

    Now the typo is fixed, the service starts and stops properly.

    Regards

    Ian
  • Aahz at Sep 24, 2010 at 4:56 am
    In article <mailman.376.1283440944.29448.python-list at python.org>,
    Ian Hobson wrote:
    I am attempting to create a Windows Service in Python.
    BTW, you probably want to subscribe to
    http://mail.python.org/mailman/listinfo/python-win32
    --
    Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/

    "If you think it's expensive to hire a professional to do the job, wait
    until you hire an amateur." --Red Adair

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 2, '10 at 3:22p
activeSep 24, '10 at 4:56a
posts11
users7
websitepython.org

People

Translate

site design / logo © 2022 Grokbase