如何让 Django 在特定时间自动执行某些操作?
例如,我的 django 应用程序必须在预先定义的时间通过 ftp 上传到远程服务器。 ftp 服务器地址、用户名、密码、时间、日期和频率已在 django 模型中定义。
我想根据模型中存储的值自动运行文件上传。
一种方法是编写一个 python 脚本并将其添加到 crontab 中。该脚本每分钟运行一次,并关注模型中定义的时间值。
我粗略地想到的另一件事可能是 django 信号。我不确定他们是否能处理这个问题。有没有办法在预定义的时间生成信号(还没有深入阅读它们)。
How to make Django execute something automatically at a particular time.?
For example, my django application has to ftp upload to remote servers at pre defined times. The ftp server addresses, usernames, passwords, time, day and frequency has been defined in a django model.
I want to run a file upload automatically based on the values stored in the model.
One way to do is to write a python script and add it to the crontab. This script runs every minute and keeps an eye on the time values defined in the model.
Other thing that I can roughly think of is maybe django signals. I'm not sure if they can handle this issue. Is there a way to generate signals at predefined times (Haven't read indepth about them yet).
发布评论
评论(4)
仅供记录 - 还有 celery 允许为将来的调度安排消息。然而,它与 cron 不同,因为它需要/使用 RabbitMQ 和用于消息队列。
Just for the record - there is also celery which allows to schedule messages for the future dispatch. It's, however, a different beast than
cron
, as it requires/uses RabbitMQ and is meant for message queues.我最近一直在思考这个问题,发现 django-cron 看起来好像会做你想做的事。
编辑:此外,如果您不是专门寻找基于 Django 的解决方案,我最近使用了 scheduler.py< /a>,这是一个小型的单文件脚本,运行良好且易于使用。
I have been thinking about this recently and have found django-cron which seems as though it would do what you want.
Edit: Also if you are not specifically looking for Django based solution, I have recently used scheduler.py, which is a small single file script which works well and is simple to use.
我在 django-chronograph 方面获得了非常好的体验。
您需要设置一个 crontab 任务:调用 chronograph python 管理命令,然后根据可管理调整的计划运行其他自定义管理命令
I've had really good experiences with django-chronograph.
You need to set one crontab task: to call the chronograph python management command, which then runs other custom management commands, based on an admin-tweakable schedule
您描述的问题最好使用 cron 解决,而不是直接使用 Django。由于您似乎需要在数据库中存储有关 ftp 上传的数据(使用 Django 访问它以获取日志或图表或其他内容),因此您可以创建一个使用 Django 的 python 脚本,该脚本通过 cron 运行。
James Bennett 写了一篇关于如何执行此操作的精彩文章,您可以在此处阅读全文: http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/
它的主要要点是,你可以编写独立的django cron 可以定期启动和运行的脚本,这些脚本可以充分利用您的 Django 数据库、模型以及它们想要的任何其他内容。这使您可以灵活地运行所需的任何代码并填充数据库,同时不会尝试让 Django 做一些它不应该做的事情(Django 是一个 Web 框架,并且是事件驱动的,而不是时间驱动的)。
祝你好运!
The problem you're describing is best solved using cron, not Django directly. Since it seems that you need to store data about your ftp uploads in your database (using Django to access it for logs or graphs or whatever), you can make a python script that uses Django which runs via cron.
James Bennett wrote a great article on how to do this which you can read in full here: http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/
The main gist of it is that, you can write standalone django scripts that cron can launch and run periodically, and these scripts can fully utilize your Django database, models, and anything else they want to. This gives you the flexibility to run whatever code you need and populate your database, while not trying to make Django do something it wasn't meant to do (Django is a web framework, and is event-driven, not time-driven).
Best of luck!