简体   繁体   English

创建一个python win32服务

[英]Creating a python win32 service

I am currently trying to create a win32 service using pywin32. 我目前正在尝试使用pywin32创建win32服务。 My main point of reference has been this tutorial: 我的主要参考点是本教程:

http://code.activestate.com/recipes/551780/ http://code.activestate.com/recipes/551780/

What i don't understand is the initialization process, since the Daemon is never initialized directly by Daemon(), instead from my understanding its initialized by the following: 我不明白的是初始化过程,因为守护进程永远不会被Daemon()直接初始化,而是我的理解它由以下内容初始化:

mydaemon = Daemon
__svc_regClass__(mydaemon, "foo", "foo display", "foo description")
__svc_install__(mydaemon)

Where svc_install , handles the initalization, by calling Daemon. 其中svc_install通过调用守护进程来处理初始化 init () and passing some arguments to it. init ()并将一些参数传递给它。

But how can i initialize the daemon object, without initalizing the service? 但是,如何在不使用服务的情况下初始化守护进程对象? I want to do a few things, before i init the service. 在我启动服务之前,我想做一些事情。 Does anyone have any ideas? 有没有人有任何想法?

class Daemon(win32serviceutil.ServiceFramework):
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcDoRun(self):
        self.run()

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def start(self):
        pass

    def stop(self):
        self.SvcStop()

    def run(self):
        pass

def __svc_install__(cls):
    win32api.SetConsoleCtrlHandler(lambda x: True, True)
    try:
        win32serviceutil.InstallService(
            cls._svc_reg_class_,
            cls._svc_name_,
            cls._svc_display_name_,
            startType = win32service.SERVICE_AUTO_START
            )
        print "Installed"
    except Exception, err:
        print str(err)

def __svc_regClass__(cls, name, display_name, description):

    #Bind the values to the service name
    cls._svc_name_ = name
    cls._svc_display_name_ =  display_name
    cls._svc_description_ = description
    try:
        module_path = sys.modules[cls.__module__].__file__
    except AttributeError:
        from sys import executable
        module_path = executable
    module_file = os.path.splitext(os.path.abspath(module_path))[0]
    cls._svc_reg_class_ = '%s.%s' % (module_file, cls.__name__)

I just create a simple "how to" where the program is in one module and the service is in another place, it uses py2exe to create the win32 service, which I believe is the best you can do for your users that don't want to mess with the python interpreter or other dependencies. 我只是创建一个简单的“如何”程序在一个模块中,服务在另一个地方,它使用py2exe创建win32服务,我相信这是你可以为你不想要的用户做的最好的搞乱python解释器或其他依赖项。

You can check my tutorial here: Create win32 services using Python and py2exe 你可以在这里查看我的教程: 使用Python和py2exe创建win32服务

I've never used these APIs, but digging through the code, it looks like the class passed in is used to register the name of the class in the registry, so you can't do any initialization of your own. 我从来没有使用过这些API,但是通过代码挖掘,看起来传入的类用于在注册表中注册类的名称,因此您无法对自己进行任何初始化。 But there's a method called GetServiceCustomOption that may help: 但是有一个名为GetServiceCustomOption的方法可能会有所帮助:

http://mail.python.org/pipermail/python-win32/2006-April/004518.html http://mail.python.org/pipermail/python-win32/2006-April/004518.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM