windows 安排定期事件:Cron/Cron 替代品(包括 Celery)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6278940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:01:25  来源:igfitidea点击:

Scheduling a regular event: Cron/Cron alternatives (including Celery)

pythonwindowsdjangolinuxcron

提问by RHH

Something I've had interest in is regularly running a certain set of actions at regular time intervals. Obviously, this is a task for cron, right?

我感兴趣的事情是以固定的时间间隔定期运行一组特定的操作。显然,这是cron的任务,对吧?

Unfortunately, the Internet seems to be in a bit of disagreement there.

不幸的是,互联网似乎在那里有点分歧。

Let me elaborate a little about my setup. First, my development environment is in Windows, while my production environment is hosted on Webfaction(Linux). There is no real cron on Windows, right? Also, I use Django! And what's suggested for Django?

让我详细说明一下我的设置。首先,我的开发环境在 Windows 中,而我的生产环境托管在Webfaction(Linux) 上。Windows 上没有真正的 cron,对吧?另外,我使用Django!对 Django 有什么建议?

Celeryof course! Unfortunately, setting up Celery has been more or less a literal nightmare for me - please see Error message 'No handlers could be found for logger “multiprocessing”' using Celery. And this is only ONE of the problems I've had with Celery. Others include a socket error which it I'm the only one ever to have gotten the problem.

当然是芹菜啦!不幸的是,设置 Celery 对我来说或多或少是一个字面上的噩梦 - 请参阅使用 Celery 的错误消息“找不到记录器“多处理”的处理程序。这只是我在使用 Celery 时遇到的问题之一。其他人包括一个套接字错误,我是唯一一个遇到过这个问题的人。

Don't get me wrong, Celery seems REALLY cool. Unfortunately, there seems to be a lack of support, and some odd limitations built into its preferred backend, RabbitMQ. Unfortunately, no matter how cool a program is, if it doesn't work, well, it doesn't work!

不要误会我的意思,Celery 看起来真的很酷。不幸的是,似乎缺乏支持,并且在其首选后端RabbitMQ 中内置了一些奇怪的限制。不幸的是,无论程序多么酷,如果它不起作用,那么它就不起作用!

That's where I hope all of you can come in. I'd like to know about cron or a cron-equivalent, which can be set up similarly (preferably identically) in both a Windows and a Linux environment.

这就是我希望你们所有人都能进来的地方。我想知道 cron 或 cron 等效项,它们可以在 Windows 和 Linux 环境中进行类似(最好是相同)设置。

(I've been struggling with Celery for about two weeks now and unfortunately I think it's time to toss in the towel and give up on it, at least for now.)

(我已经与 Celery 苦苦挣扎了大约两个星期,不幸的是,我认为是时候认输并放弃它了,至少现在是这样。)

回答by mrmagooey

I had the same problem, and held off trying to solve it with celery (too complicated) or cron (external to application) and ended up finding Advanced Python Scheduler. Only just started using it but it seems reasonably mature and stable, has decent documentation and will take a number of scheduling formats (e.g. cron style).

我遇到了同样的问题,并没有尝试用 celery(太复杂)或 cron(应用程序外部)解决它,最终找到了Advanced Python Scheduler。才刚刚开始使用它,但它似乎相当成熟和稳定,有不错的文档,并且将采用多种调度格式(例如 cron 样式)。

From the documentation, running a function at a specific interval.

文档中,以特定时间间隔运行函数。

from apscheduler.scheduler import Scheduler
sched = Scheduler()
sched.start()
def hello_world():
    print "hello world"
sched.add_interval_job(hello_world,seconds=10)

This is non-blocking, and I run something pretty identical by simply importing the module from my urls.py. Hope this helps.

这是非阻塞的,我通过简单地从我的urls.py. 希望这可以帮助。

回答by Brian Neal

A simple, non-Celery way to approach things would be to create custom django-admin commandsto perform your asynchronous or scheduled tasks.

一种简单的非 Celery 处理方法是创建自定义 django-admin 命令来执行异步或计划任务。

Then, on Windows, you use the atcommand to schedule these tasks. On Linux, you use cron.

然后,在 Windows 上,您可以使用该at命令来安排这些任务。在 Linux 上,您使用cron.

I'd also strongly recommend ditching Windows if you can for a development environment. Your life will be so much better on Linux or even Mac OSX. Re-purpose a spare or old machine with Ubuntu for example, or run Ubuntu in a VM on your Windows box.

如果可以的话,我还强烈建议您放弃 Windows 以用于开发环境。在 Linux 甚至 Mac OSX 上你的生活会好很多。例如,使用 Ubuntu 重新使用备用或旧机器,或者在 Windows 机器上的 VM 中运行 Ubuntu。

回答by Andy Baker

https://github.com/andybak/django-cron

https://github.com/andybak/django-cron

Triggered by a single cron task but all the scheduling and configuration is done in Python.

由单个 cron 任务触发,但所有调度和配置均在 Python 中完成。

回答by Noel Puru

Django Chronographis a great alternative. You only need to setup one cron then do everything in django admin. You can schedule tasks/commands from django management.

Django Chronograph是一个很好的选择。您只需要设置一个 cron,然后在 django admin 中完成所有操作。您可以从 Django 管理中安排任务/命令。