A Final Service
We have made a number of changes to the sample service since first presented. Here’s the full source code:
# PipeService2.py # # A sample demonstrating a service which uses a # named-pipe to accept client connections, # and writes data to the event log. import win32serviceutil import win32service import win32event import win32pipe import win32file import pywintypes import winerror import perfmon import os class PipeService(win32serviceutil.ServiceFramework): _svc_name_ = "PythonPipeService" _svc_display_name_ = "A sample Python service using named pipes" 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) # We need to use overlapped IO for this, so we dont block when # waiting for a client to connect. This is the only effective way # to handle either a client connection, or a service stop request. self.overlapped = pywintypes.OVERLAPPED() # And create an event to be used in the OVERLAPPED object. self.overlapped.hEvent = win32event.CreateEvent(None,0,0,None) # Finally initialize our Performance Monitor counters self.InitPerfMon() def InitPerfMon(self): # Magic numbers (2 and 4) must match header and ini file used # at install - could lookup ini, but then Id need it a runtime # A counter for number of connections per second. self.counterConnections=perfmon.CounterDefinition(2) # We ...
Get Python Programming On Win32 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.