python - 将参数传递给 python 服务

标签 python service

我需要一些关于 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);

就像现在一样,循环将执行固定次数。我想要的是以某种方式简单地将值发送给服务。真的不在乎如何。

最佳答案

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

关于python - 将参数传递给 python 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3000476/

相关文章:

python - 哪个 Python IDE 具有 Visual Studio 功能?

python - "Stale file handle"错误,当进程尝试读取文件时,其他进程已删除

python - 如何将xml xpath解析为列表python

symfony - 服务中的 Simfony2 服务尝试调用方法 "get"

java.lang.IllegalThreadStateException : Thread already started while there is only 1 thread 异常

c# - 崩溃时自动重新启动自托管 wcf 服务

Android 在服务中使用 AsyncTaskLoader

python - 使用 TensorFlow 和 tf.data.Dataset 从文件夹中采样图像序列

python - 从 TfidfVectorizer 获取全文

java - 当音乐应用程序通知栏中的歌曲更改时,歌曲名称和专辑封面不会更改