2010-06-08 236 views
3

我需要一些python服务的帮助。将参数传递给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) 

这是在主脚本:

import service.WindowsService, win32serviceutil 
win32serviceutil.HandleCommandLine(service.WindowsService.LoopService); 

由于它是目前,循环将执行次数固定量。我想只是简单地将价值发送到服务。真的不在乎如何。

回答

0

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

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

- 在问题编辑后添加。

更清晰,但仍然不足。您需要查看win32serviceutil.HandleCommandLine并查看它如何调用service.WindowsService.LoopService .__ init__。特别是,如何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) 

然后你就可以控制重复的次数改变LoopService.repetitions 之前创建一个新的实例。

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

这是有效的,但它很丑。尝试控制参数,然后相应地设置self.repetition

+0

我编辑了我的帖子,以包含一个希望给出更多信息的例子。 – Grim 2010-06-09 00:16:49

0

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