The servicemanager Module
As mentioned previously in this chapter,
PythonService.exe hosts all Python programs
running as services. Once this host .exe is
running, it makes a module servicemanager
available to the Python program. Because your Python scripts are used
both by Python.exe (when installing, starting,
or debugging the service) and by
PythonService.exe (when running as a service),
you can’t import this module from the top level of your Python
program; the import
servicemanager statement will fail for
Python.exe. In practice, this means you should
only import this module locally to a function.
The primary purpose of servicemanager is to
provide facilities for interacting with the SCM at runtime. Most of
this interaction is done by the base class
win32serviceutil.ServiceFramework(), so in general
this is covered for you.
There are, however, some useful utility functions you may wish to
use. Among these is a set of functions for writing to the Event Log.
The most general purpose is the function
LogMsg()
, which takes the following parameters:
-
ErrorType One of
EVENTLOG_INFORMATION_TYPE,EVENTLOG_ERROR_TYPE, orEVENT-LOG_WARNING_TYPE. These constants can be found inservicemanager.-
ErrorID The message ID. This uniquely identifies the message text.
-
Inserts = None A list of string inserts for the message or
None. The inserts are merged with the message text to provide the final result for the user.
There are three other Event Log-related functions in this module.
LogInfoMsg()
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access