不同之处在于,我更喜欢可能更精确和低负载的东西(每分钟的 cron 作业对于那些人来说并不可取)并且开销最小(即用 rabbitmq 安装 celery 似乎是一个很大的矫枉过正)。
此类任务的一个示例是个人提醒服务器(带有可以通过网络编辑并通过电子邮件或 XMPP 发送的提醒)。
我可能正在寻找更像 node.js 的 setTimeout 的东西,但对于 django(虽然我可能更喜欢在 node.js 中实现提醒,但这仍然是一个可能有趣的问题)。
例如,可以在django 应用程序中启动新线程(使用由sleep() 和send() 组成的函数);这在哪些方面可能很糟糕?
最佳答案
将线程用于此解决方案的问题是 Python 线程的典型问题,它总是驱使人们转向多进程解决方案。由于您的线程不是由正常的请求-响应周期驱动的,因此问题在这里变得更加复杂。 Malcolm Tredinnick 很好地总结了这一点 here :
Have to disagree. Threads are not a good solution to this problem. The issue is process management. As written, your threads will never be rejoined. Webserver processes have a lifecycle uncontrollable by you (the MaxRequestsPerChild Apache parameter and similar things in other servers) and you are messing with that by using threads.
If you need a process with a lifecycle that is not matched by the request-response path — something long running and independent of the response — a completely separate process is definitely the right model to use. Using a thread is tying it to the response lifecycle, which wil have unintended side-effects.
一个可能的解决方案是让一个长时间运行的进程执行您的任务,该进程从轻量级 cron 进程获取唤醒信号。
另一种可能性是使用 0mq 构建一些东西,这比 AMQP 样式队列轻得多(当然以某些功能为代价)。 Tarek Ziade 正在从事名为 powerhose 的 Mozilla 项目它使用 0mq,看起来 super 简单,并且具有精确到秒的心跳功能。
关于python - 最简单的 django 定时/计划任务(例如 : reminders)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9168359/