简体   繁体   English

如何使用每5秒运行一次的Python Win32com创建计划任务?

[英]How do I create a scheduled task using Python Win32com that runs every 5 seconds?

I am trying to create a scheduled task in Python using Win32com. 我正在尝试使用Win32com在Python中创建计划任务。 I am able to create a daily trigger. 我能够创建每日触发器。 However, I cannot find a way to create a trigger every 5 seconds or every minute for that matter. 但是,我无法找到一种方法来每5秒或每分钟创建一次触发器。 Does anybody have any pointers on how to do that? 有人对如何做到这一点有任何指示吗?

As said in a comment, if you want to do stuff with this frequency you are better off just having your program run forever and do its own scheduling. 如评论中所述,如果您想以这种频率执行操作,最好让程序永久运行并执行自己的调度。

In a similar fashion to @Barmak Shemirani's answer, but without spawning threads: 以与@Barmak Shemirani的回答类似的方式,但没有产生线程:

import time

def screenshot():
    # do your screenshot...

interval = 5.
target_time = time.monotonic() + interval
while True:
    screenshot()
    delay = target_time - time.monotonic()
    if delay > 0.:
        time.sleep(delay)
    target_time += interval

or, if your screenshot is fast enough and you don't really care about precise timing: 或者,如果您的屏幕截图足够快并且您实际上并不在乎精确的时间:

while True:
    screenshot()
    time.sleep(interval)

If you want this to run from the system startup, you'll have to make it a service , and change the exit condition accordingly. 如果要从系统启动时开始运行,则必须使其成为service ,并相应地更改退出条件。

pywin32 is not required to create schedule or timer. pywin32不需要创建时间表或计时器。 Use the following: 使用以下内容:

import threading

def screenshot():
    #pywin32 code here
    print ("Test")

def starttimer():
  threading.Timer(1.0, starttimer).start()
  screenshot()

starttimer()

Use pywin32 for taking screenshot etc. 使用pywin32截取屏幕截图等。

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

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