Home > Backend Development > Python Tutorial > Can Python Programs Be Run as Windows Services?

Can Python Programs Be Run as Windows Services?

Barbara Streisand
Release: 2024-12-02 22:30:11
Original
137 people have browsed it

Can Python Programs Be Run as Windows Services?

Running Python Scripts as Windows Services

The need to manage interrelated objects stored in a database necessitates a service that provides a higher level interface for operations on those objects. However, concerns arise regarding running Python programs as a Windows service to enable automatic execution without user login.

Is it Possible to Run Python Programs as a Windows Service?

Yes, it is possible to run Python programs as a Windows service using the pythoncom libraries included in ActivePython or installable with pywin32.

Sample Implementation

A basic skeleton of a simple service:

import win32serviceutil
import win32service
import win32event
import servicemanager

class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "Test Service"

    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)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()

    def main(self):
        pass

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)
Copy after login

Windows Service Awareness and Management

Windows is aware of the service through win32serviceutil.ServiceFramework and servicemanager modules.

You can manage the service using the native Windows utilities such as the Services applet in the Control Panel or through the command line with commands like:

  • net start servicename
  • net stop servicename
  • sc queryex servicename

Analogue to /etc/init.d

There is no direct equivalent to putting a start/stop script in /etc/init.d in Windows. Windows services are typically configured in the Windows Registry. However, you can utilize service control managers like SystemD or Supervisor to achieve similar functionality.

The above is the detailed content of Can Python Programs Be Run as Windows Services?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template