django 中长期运行进程的全局状态
我需要启动一个长期运行的进程来响应 django 中的表单提交。然后,我希望能够使用 ajax 进行轮询,并让视图响应进程的状态(启动、停止或运行)。此外,我希望能够停止该过程。
所以我的观点是这样的:
def start()
. . .
def stop()
. . .
def status()
. . .
这个特定的问题已经在 SO 上被多次解决,但我的情况略有不同: 我正在寻找一个完全 pythonic 的解决方案,不需要任何在库存 django 中找不到的东西,并且不需要使用数据库。
我发现的最接近的是这篇文章。我已经实现了代码,并对其进行了相当多的简化,但事实证明 request.session 不是全局的(两个浏览器有不同的会话)。
我的问题可能可以通过使用 request.session 以外的其他方法来保存开始/停止/状态状态来解决,但我不知道那会是什么。
我在阅读中遇到的另一种可能性是 django 中间件,但根据
__init__() 仅在服务器启动时调用一次,不适用于个人 请求
还有一篇博文这里这谈论了 Django 中的全局状态。 有什么想法吗?
I have the need to kick off a long-running process in response to a form submission in django. Then, I'd like to be able to poll using ajax and have the view respond with the state of the process (started, stopped, or running). Additionally, I want to be able to stop the process.
so my view looks like this:
def start()
. . .
def stop()
. . .
def status()
. . .
This particular question has been addressed multiple times on S.O., but my situation is subtly different: I am looking for a solution that is entirely pythonic, does not require anything not found in stock django, and does not require the use of a database.
The closest thing I have found is this post. I have implemented the code, and streamlined it quite a bit, but it turns out that request.session is not global (two browsers have different sessions).
It might be that my problem could be solved by using something other than request.session to save the start/stop/status state, but I have no idea what that would be.
Another possibility I came across in my reading is django middleware, but according to this,
__init__() is called only once — at server startup — not for individual
requests
There is also a blog post here that talks about global state in django.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果需要从任何进程访问状态,并且您不想将其存储在数据库中(我不明白为什么不这样做,这是最好的地方),您可以将其存储在临时文件中文件系统。
If the state needs to be accessible from any process, and you don't want to store it in the database (and I can't see why not, that's the best place for it), you could store it in a temporary file on the filesystem.
一种方法是启动一个单独的线程并通过线程模块监视它。
One approach would be to kick off a separate thread and monitor it via the threading module.
您需要使用任务队列框架。试试这个: http://ask.github.com/celery/index.html
You need to use a task queue framework. Try this: http://ask.github.com/celery/index.html