16

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.

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.

That's what I'm trying to do when using 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?

4

4 回答 4

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.

Edit: cron calls the relevant (and custom) django command to execute the task.

于 2009-10-24T22:57:01.877 回答
5

首先 - 尝试为你的任务使用 cron,如早说 shanyu。

如果它不适合您 - 然后尝试使用CeleryProject,用于 Django 的任务队列。为了工作,它使用RabbitMQ。这里是简单使用基础期货的一些概述

于 2009-10-25T10:36:05.147 回答
2

查看 kronos.py 中的代码以了解解决此问题的方法。

http://www.razorvine.net/download/kronos.py

于 2009-10-25T10:48:26.697 回答
2

http://code.google.com/p/django-command-extensions/wiki/JobsScheduling

是一个不错的库,您可以使用它来完成此任务。

于 2009-10-25T00:29:16.897 回答