使用 Python 的 Schedule 模块调度任务 [英] Scheduling tasks using Python's Schedule module

查看:80
本文介绍了使用 Python 的 Schedule 模块调度任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自文档:

import schedule
import time

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

我知道当程序运行时,它会执行您告诉它运行的功能.我不明白的是,您将如何将此作为每天的自动化任务.您是否会从命令行调用它并始终保持打开状态?如果我关闭计算机,我将不得不再次重新启用它,不是吗?

I understand that while the program is running, it will do the function you tell it to run. What I don't understand is how you would go about making this an automated task for every day. Is the idea that you would call this from the command line and always leave that open? If I shut off my computer, I would have to re-enable that again wouldn't I?

在这种情况下,我觉得在创建自动化 Python 任务时缺少某些东西.我在 Windows 环境中.

I feel there is something I am missing when creating an automated Python task in this case. I am on a windows environment.

推荐答案

这里是概述:将任务作为启动项运行在每个操作系统上意味着不同的事情,这与 python 无关.

Here is the overview: Running tasks as startup items means different things on each OS which has nothing to do with python specifically.

  • 在 Windows 上,您可以通过使用 python 库 Pyinstaller(它将您的脚本更改为 .exe 文件,然后运行 ​​your.exe install --startup='auto'

在基于 Linux 的操作系统上,您需要检查脚本的放置位置,因为 最近几年启动顺序发生了变化.甚至还有管理软件包可以让您更轻松.

On Linux based OS's you would need to check where to put the script because the startup sequence has changed in the last few years. There are even management software packages to make it easier.

在 mac 上有用于控制启动服务的 GUI 工具以及 launchctl http://www.macworld.com/article/2047747/take-control-of-startup-and-login-items.html

On mac there is the GUI tools for controlling startup services as well as launchctl http://www.macworld.com/article/2047747/take-control-of-startup-and-login-items.html

您可以通过以下方式查看当前计算机上的进程:

You can take a look at the process currently on your computer by going to:

  • Windows:任务管理器(按 ctrl-alt-delete 并选择任务管理器)

  • Windows: Task manager (press ctrl-alt-delete and select Task manager)

  • (取决于您的 Windows 版本)单击详细信息"选项卡.如果它作为系统进程运行,您将看到用户名为空或具有系统".

Linux 或 Mac:在终端类型 ps -Al

Linux or Mac: in a terminal type ps -Al

系统级别 - 如果没有人登录,您的计算机在做什么?(你的脚本?,网络服务器?,蛋白质折叠?,梦见电羊?)

System level - if nobody is logged in what is your computer doing? (your script?, web server?, protein folding?, dreaming of electric sheep?)

是的,每次运行单独的脚本时,Python 都会占用资源.我有大量的 RAM 和 Python 需要 <30 MB 来运行每个脚本(取决于库的大小 + 程序的大小 + io 绑定 + cpu 绑定问题).您的系统当前正在运行超过 100 个进程,并且能够运行 1000 个进程.不要担心在系统上优化您的程序,直到出现问题.

Yes, Python would be taking up resources each time you run a separate script. I have Gigs of RAM and Python takes <30 MB to run each script (depending on the size of libraries + size of program+ io bound + cpu bound problems). Your system is running >100 processes currently and it able to run 1000's. Don't worry about optimizing your program on the system till it's a problem.

这篇关于使用 Python 的 Schedule 模块调度任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆