向python服务传递参数

向python服务传递参数,python,service,Python,Service,我需要一些关于python服务的帮助 我有一个用Python编写的服务。我需要做的是给它传递一些参数。让我给你举个例子来解释一下 假设我有一个服务,它什么也不做,只是将一些东西写入日志。我想在日志中多次写入相同的内容,因此使用循环。我想在开始服务时通过循环计数器,但我不知道如何通过。我以以下方式启动服务: win32serviceutil.HandleCommandLine(WinService) 我在找像这样的东西 win32serviceutil.HandleCommandLine(Win

我需要一些关于python服务的帮助

我有一个用Python编写的服务。我需要做的是给它传递一些参数。让我给你举个例子来解释一下

假设我有一个服务,它什么也不做,只是将一些东西写入日志。我想在日志中多次写入相同的内容,因此使用循环。我想在开始服务时通过循环计数器,但我不知道如何通过。我以以下方式启动服务:

win32serviceutil.HandleCommandLine(WinService)
我在找像这样的东西

win32serviceutil.HandleCommandLine(WinService,10)
我真的不在乎它是怎么做的,只要我能给它传递参数就行。在一天中的大部分时间里,我都在努力让这一切顺利进行,但运气不佳。此外,服务不是直接运行的,而是导入的,然后从那里运行

编辑:

下面是一个例子,希望它能澄清一些问题

这在WindowsService.py中:

import win32serviceutil, win32service, win32event, servicemanager, win32serviceutil

class LoopService(win32serviceutil.ServiceFramework):
    _svc_name_ = "LoopService"
    _svc_description_ = "LoopService"
    _svc_display_name_ = "LoopService" 

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

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

    def SvcDoRun(self):
        i = 0;
        while i < 5:
            servicemanager.LogInfoMsg("just something to put in the log");
            i += 1
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

现在,循环将执行固定的次数。我想要的是以某种方式将值发送到服务。真的不在乎怎么做。

对不起,没有足够的信息来回答你的问题。这似乎是特定于应用程序的事情

我唯一能想到的就是查看win32serviceutil.HandleCommandLine方法和WinService类的代码,以确定哪个类写入日志。然后,您必须创建一个子类并重写负责写入日志的方法,以接收附加参数。最后,您必须将原始类中的所有引用都转换为新类

--在问题版本之后添加

更清楚,但仍然不够。您需要查看win32serviceutil.HandleCommandLine并查看它如何调用service.WindowsService.LoopService.\uuuu init\uuuu。特别是HandleCommandLine如何生成参数以及如何控制参数

如果您赶时间,您可以:

class LoopService(win32serviceutil.ServiceFramework):
    repetitions = 5
    # ... 

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

    # ...

    def SvcDoRun(self):
        for i in range(self.repetitions):
            servicemanager.LogInfoMsg("just something to put in the log");
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
然后,您可以在创建新实例之前控制重复次数

import service.WindowsService, win32serviceutil
service.WindowsService.LoopService.repetitions = 10
win32serviceutil.HandleCommandLine(service.WindowsService.LoopService);

这很管用,但很难看。尝试控制args,然后相应地设置self.repeation

我认为您不能将参数直接传递给服务。您可能可以使用环境(在启动服务之前设置环境变量并从服务中读取它)。

我编辑了我的文章,其中包含了一个示例,希望能提供更多信息。
import service.WindowsService, win32serviceutil
service.WindowsService.LoopService.repetitions = 10
win32serviceutil.HandleCommandLine(service.WindowsService.LoopService);