I need to run a process that might take hours to complete from a Django view. I don't need to know the state or communicate with it but I need that view to redirect away right after starting the process.
我需要运行一个可能需要几个小时才能从Django视图完成的过程。我不需要知道状态或与之通信,但我需要该视图在启动过程后立即重定向。
I've tried using subprocess.Popen
, using it within a new threading.Thread
, multiprocessing.Process
. However, the parent process keeps hanging until child terminates. The only way that almost gets it done is using a fork. Obviously that isn't good as it leaves a zombie process behind until parent terminates.
我尝试过使用subprocess.Popen,在新的threading.Thread,multiprocessing.Process中使用它。但是,父进程一直挂起,直到子进程终止。几乎完成它的唯一方法是使用fork。显然,这是不好的,因为它留下了一个僵尸进程,直到父终止。
That's what I'm trying to do when using fork:
这就是我在使用fork时要做的事情:
if os.fork() == 0:
subprocess.Popen(["/usr/bin/python", script_path, "-v"])
else:
return HttpResponseRedirect(reverse('view_to_redirect'))
So, is there a way to run a completely independent process from a Django view with minimal casualties? Or am I doing something wrong?
那么,有没有办法从Django视图运行完全独立的进程,伤亡最小?或者我做错了什么?
4 个解决方案
#1
10
I don't know if this will be suitable for your case, nevertheless here is what I do: I use a task queue (via a django model); when the view is called, it enters a new record in the tasks and redirects happily. Tasks in turn are executed by cron on a regular basis independently from django.
我不知道这是否适合你的情况,不过这是我的工作:我使用任务队列(通过django模型);调用视图时,它会在任务中输入新记录并快乐地重定向。反过来的任务由cron定期执行,独立于django。
Edit: cron calls the relevant (and custom) django command to execute the task.
编辑:cron调用相关(和自定义)django命令来执行任务。
#2
5
First of all - try to using cron for you task, as early say shanyu.
首先 - 尝试使用cron为你完成任务,如早期说shanyu。
If it doesn't suit you - then try to use CeleryProject, for task Queue for Django. For working it uses RabbitMQ. And here is a little overview for simple using of basing futures
如果它不适合你 - 那么尝试使用CeleryProject,用于Django的任务队列。为了工作,它使用RabbitMQ。这里有一个简单使用基础期货的概述
#3
2
http://code.google.com/p/django-command-extensions/wiki/JobsScheduling
http://code.google.com/p/django-command-extensions/wiki/JobsScheduling
Is a nice library that that you can use to accomplish this task.
是一个很好的库,可以用来完成这个任务。
#4
2
Take a look at the code in kronos.py to see one solution to this problem.
看看kronos.py中的代码,看看这个问题的一个解决方案。
http://www.razorvine.net/download/kronos.py
http://www.razorvine.net/download/kronos.py
#1
10
I don't know if this will be suitable for your case, nevertheless here is what I do: I use a task queue (via a django model); when the view is called, it enters a new record in the tasks and redirects happily. Tasks in turn are executed by cron on a regular basis independently from django.
我不知道这是否适合你的情况,不过这是我的工作:我使用任务队列(通过django模型);调用视图时,它会在任务中输入新记录并快乐地重定向。反过来的任务由cron定期执行,独立于django。
Edit: cron calls the relevant (and custom) django command to execute the task.
编辑:cron调用相关(和自定义)django命令来执行任务。
#2
5
First of all - try to using cron for you task, as early say shanyu.
首先 - 尝试使用cron为你完成任务,如早期说shanyu。
If it doesn't suit you - then try to use CeleryProject, for task Queue for Django. For working it uses RabbitMQ. And here is a little overview for simple using of basing futures
如果它不适合你 - 那么尝试使用CeleryProject,用于Django的任务队列。为了工作,它使用RabbitMQ。这里有一个简单使用基础期货的概述
#3
2
http://code.google.com/p/django-command-extensions/wiki/JobsScheduling
http://code.google.com/p/django-command-extensions/wiki/JobsScheduling
Is a nice library that that you can use to accomplish this task.
是一个很好的库,可以用来完成这个任务。
#4
2
Take a look at the code in kronos.py to see one solution to this problem.
看看kronos.py中的代码,看看这个问题的一个解决方案。
http://www.razorvine.net/download/kronos.py
http://www.razorvine.net/download/kronos.py