Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to run Python programs in the way of Windows Service

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to run the Python program in the way of Windows Service. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Python program code

Importwmi importos c=wmi.WMI () watcher=c.Win32_PowerManagementEvent.watch_for (EventType=7) # statement to monitor standby events; whileTrue: os.system ("kdlj.vbs") # runs the program "connect to broadband". Here you still use the vbs code of that guy last time; watcher ()

Since the console window of the Python program is there all the time, it seems to be a bit of a hindrance. So I thought that if I could run it as windowsservice, just like any other program in the windows service manager.

Finally, I found an introduction in the book "PythonProgrammingOnWin32" (byMarkHammond).

There is a simple template, just put the Python program code in the appropriate location:

# SmallestService.py

#

# AsampledemonstratingthesmallestpossibleservicewritteninPython.

Importwin32serviceutil

Importwin32service

Importwin32event

ClassSmallestPythonService (win32serviceutil.ServiceFramework):

_ svc_name_= "SmallestPythonService"

_ svc_display_name_= "ThesmallestpossiblePythonService"

Def__init__ (self,args):

Win32serviceutil.ServiceFramework.__init__ (self,args)

# Createaneventwhichwewillusetowaiton.

# The "servicestop" requestwillsetthisevent.

Self.hWaitStop=win32event.CreateEvent (None,0,0,None)

DefSvcStop (self):

# Beforewedoanything,telltheSCMwearestartingthestopprocess.

Self.ReportServiceStatus (win32service.SERVICE_STOP_PENDING)

# Andsetmyevent.

Win32event.SetEvent (self.hWaitStop)

DefSvcDoRun (self):

# put your program code here and OK it

Win32event.WaitForSingleObject (self.hWaitStop,win32event.INFINITE)

If__name__=='__main__':

Win32serviceutil.HandleCommandLine (SmallestPythonService)

# the name in parentheses can be changed to something else, which must be consistent with the class name

Next, just install the service and run: SmallestService.pyinstall under cmd.

In this way, you can find a service called "ThesmallestpossiblePythonService" in the windows service manager. If it is set to start automatically, it will boot automatically and run in the background all the time. Out of sight, out of mind,)

However, although this has achieved its goal, it still finds a small problem, that is, if you want to stop the service, the closed progress bar will stand still, and you must turn off pythonservice.exe in the process manager. This bug has always been unable to solve, that is, when you shut down the service, you have to cancel the monitoring standby events, otherwise you can't get out of this endless cycle.

The above is the editor for you to share how to run the Python program in the way of Windows Service, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report