如何在python / django中分叉一个进程? [英] How to fork a process in python/django?

查看:102
本文介绍了如何在python / django中分叉一个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Python的一般问题,但是在django的上下文中。

This is more of a Python general question however in a context of django.

现在我在django中有这个视图,它必须处理大量的数据。通常它需要服务器(nginx与django运行在代理使用)几分钟的时间。有时服务器超时。我不想增加nginx的超时时间。我意识到,如果我可以在django视图中的python中分配一个进程,以便分叉(子)进程将执行独立于django视图的所有数据处理,则视图将能够立即将该请求返回给用户因此永远不会超时),子进程将继续运行在后台完成所有计算。

For now I have this view in django which has to process a lot of data. Usually it takes the server (nginx with django running in proxy using) a couple of minutes to do it. Sometimes the server times out. I don't want to increase the time-out time in nginx. I realize that if I can fork a process in python in the django view so that the forked (child) process will do all the data crunching independently of the django view, then the view would be able to return the request to the user immediately (therefore never timing-out) and the child process would continue running in the background finishing up all the calculation.

所以这里是一个问题:

如何在python中分叉一个独立的进程(如果可能的话,python代码在同一个文件中)?如果可能,我可以如何分配一个unix进程优先级别?

How can I fork an independent process in python (and if possible for the python code to be in the same file)? And if possible how can I assign a unix process priority level to it?

我看了一些在python中分配进程的方法,似乎有一些选项。哪一个最适合这种情况?

I looked at some of the ways of forking a process in python and it seems there are a few options. Which one is the best appropriate for this scenario?

谢谢。

推荐答案

最佳实践的答案是使用队列管理器,通常是RabbitMQ或Django-celery处理的任何后端。

the 'best practice' answer is to use a queue manager, typically RabbitMQ or any backend handled by Django-celery.

仍然有一些较轻的选项这样做会产生一个新的线程。这些选项通常缺少的是跟踪进度的一些方法,或保持线程数量的控制。

Still, there are a few lighter options that do spawn a new thread. what these options usually lack is some way to track progress, or keep the number of threads under control.

检查 Django-utils 查看是否够用。如果没有,请去芹菜。

check Django-utils to see if it's enough. if not, go for Celery.

这篇关于如何在python / django中分叉一个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆