Django 可以在预定义的时间自动执行某些操作,而不是使用 crontab

发布于 2024-10-03 11:23:28 字数 302 浏览 3 评论 0 原文

如何让 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蒗幽 2024-10-10 11:23:28

仅供记录 - 还有 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.

无尽的现实 2024-10-10 11:23:28

我最近一直在思考这个问题,发现 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.

流年已逝 2024-10-10 11:23:28

我在 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

心在旅行 2024-10-10 11:23:28

您描述的问题最好使用 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文