如何在 Python 服务中刷新 if 语句



好的,所以我做了一个python服务,它获取当前时间,并根据目录中的所有文件进行检查。如果该目录中的文件与当前时间相同,则应读取该文件并按照其中的命令进行操作。例如:如果当前时间是7:30,那么如果它在"Time"目录中找到一个名为"7_30.protocol"(可能是"07_30.protocol")的文件,它应该读取它并执行里面的命令。如果这是当前时间,它可以正常工作,但如果不是并且您必须等待时间,那么它将不起作用。这是我的代码。我做错了什么?我现在已经编辑以添加调试文件!

import win32service
import win32serviceutil
import win32event
from time import *
import glob, os, dw, winmod2
def trim_protocol(name):
     return name[:-9]
class MySvc(win32serviceutil.ServiceFramework):
    _svc_name_ = "MySvc"
    _svc_display_name_ = "My Service"
    _svc_description_ = "This is just My Service"
    debug = open("C:/LIP/ServiceDebugging.txt", "w")
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
    def SvcStop(self):
        self.debug.write("Closing service...n")
        self.debug.write("Service closed!")
        self.debug.close()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
    def SvcDoRun(self):
        import servicemanager
        self.timeout = 3000
        profile_filenames = []
        for file in os.listdir("C:/LIP/LIP Source Files/Config/Commands/Profiles/Time"):
            if file.endswith(".protocol"):
                profile_filenames.append(file)
        profile_titles = map(trim_protocol, profile_filenames)
        profiles = list(profile_titles)
        times = [i.replace('_', ':') for i in profiles]
        HD = os.getenv("SystemDrive")
        while 1:
            rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
            if rc == win32event.WAIT_OBJECT_0:
                break
            else:
                self.debug.write("Service running...n")
                current_time = strftime("%H:%M", localtime())
                if current_time in times:
                    self.debug.write("Found a file matching the current time! Opening and running...n")
                    filename = current_time.replace(":", "_")
                    filename = filename + ".protocol"
                    file = open(HD + "/LIP/LIP Source Files/Config/Commands/Profiles/Time/" + filename, "r")
                    commands = file.read()
                    exec(commands)
                    file.close()
                    current_time = strftime("%H:%M", localtime())
                    self.debug.write("Commands have been run! Sleeping till the next minute!...n")
                    sleep(60)
                else:
                    self.debug.write("No time match found! Moving on...n")
                    print("Standing by...")
                sleep(5)
                continue

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(MySvc)

告诉我您是否需要更多信息,或者这是否没有意义!

编辑:打开赏金。让我们得到答案!编辑:添加了调试文件。

  1. 首先,添加

睡眠(5)

命令在循环结束时(就在 Continue 语句之前),以便服务不会在不安分的循环:)中消耗您的 CPU

  1. 您可以在服务启动时加载协议文件的列表。 文件是否提前创建?
  2. 我会添加更多
  3. 调试信息(例如,在关键点添加更多打印命令)并从命令行运行脚本,这样我就能看到它到底卡在哪里。
  4. 这只是我的意见,你可以接受或离开它,但我强烈建议你花一些时间来提高你的 OOP 技能,你的代码对我来说看起来太程序化了:)

最新更新