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

What are the operation skills related to windows service running Python?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "what are the operation skills related to windows service running Python", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn this article "what are the operation skills related to windows service running Python".

Example

Import wmi

Import os

C = wmi.WMI ()

Watcher = c.Win32_PowerManagementEvent.watch_for

(EventType=7) # Monitoring standby events

While True:

Os.system ("kdlj.vbs") # runs the "connect to broadband" program

The vbs code of that guy last time is still used here.

Watcher ()

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

Finally, I found an introduction in the book "Python Programming On Win32" (by Mark Hammond). There is a simple template in it. Just put the program code in the appropriate place:

# SmallestService.py # # A sample demonstrating the smallest possible service written in Python. Import win32serviceutil import win32service import win32event class SmallestPythonService (win32serviceutil.ServiceFramework): _ svc_name_ = "SmallestPythonService" _ svc_display_name_ = "The smallest possible PythonService" def _ _ init__ (self, args): win32serviceutil.ServiceFramework.__init__ (self, args) # Create an event which we will use to wait on. # The "service stop" request will set this event. Self.hWaitStop = win32event.CreateEvent (None, 0,0, None) def SvcStop (self): # Before we do anything, tell the SCM we are starting the stop process. Self.ReportServiceStatus (win32service.SERVICE_STOP_PENDING) # And set my event. Win32event.SetEvent (self.hWaitStop) def SvcDoRun (self): # put your program code here and OK 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.py install under cmd. In this way, you can find a service called "The smallest possible Python Service" 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 achieves the purpose of windows service running Python, it still finds a small problem, that is, if you want to stop the service, the closed progress bar will stand still and pythonservice.exe must be turned off in the process manager.

These are all the contents of this article entitled "what are the operational skills related to windows service running Python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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