Django的长期运行任务 [英] Long running tasks with Django

查看:265
本文介绍了Django的长期运行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建一个应用程序,该应用程序将能够执行主要是长期的系统任务,例如:

My goal is to create an application that will be able to do long-lasting mainly system tasks, such as:


  • 签出存储库中的代码

  • 在各种本地化之间复制目录,

  • 等。

问题是我需要以某种方式独立于网络浏览器进行准备。我的意思是,例如,在开始签出/复制操作之后,关闭Web浏览器不会中断该操作。因此,回到该站点后,我可以看到复制继续进行,或者在关闭浏览器时又开始执行其他操作……

The problem is I need to prepare it somehow independently from the web browser. I mean that for example after starting the checkout/copy action, closing the web browser will not interrupt the action. So after going back to that site I can see that the copying goes on or another action started when the browser was closed...

我正在搜索各种工具,例如RabbitMQ + Celery,Twisted,Pyro,XML-RPC,但是我不知道这些方法是否适合我。创建Django应用时,有人遇到过类似的需求吗?请让我知道是否有任何我应该知道的方法/软件包。代码示例也将非常受欢迎!

I was searching through various tools, like RabbitMQ + Celery, Twisted, Pyro, XML-RPC but I don't know if any of these will be suitable for me. Has anyone encountered similar needs when creating Django app? Please let me know if there are any methods/packages that I should know. Code samples also will be more than welcome!

在此先感谢您的建议!

(对不起

推荐答案

基本上,您需要一个运行在请求。做到这一点的最简单的方法(至少在类似Unix的操作系统上)是 fork()

Basically you need to have a process that runs outside of the request. The absolute simplest way to do this (on a Unix-like operating system, at least) is to fork():

if os.fork() == 0:
    do_long_thing()
    sys.exit(0)
… continue with request …

这有一些缺点,但是(例如,如果服务器崩溃,漫长的事情将丢失) ……例如,芹菜可以在哪派上用场。它将跟踪需要完成的工作,工作的结果(成功/失败/无论如何),并使其易于在其他计算机上运行。

This has some downsides, though (ex, if the server crashes, the "long thing" will be lost)… Which is where, ex, Celery can come in handy. It will keep track of the jobs that need to be done, the results of jobs (success/failure/whatever) and make it easy to run the jobs on other machines.

将芹菜与Redis后端配合使用(请参阅Kombu的Redis运输工具)非常简单,所以我建议您先去那里看看。

Using Celery with a Redis backend (see Kombu's Redis transport) is very simple, so I would recommend looking there first.

这篇关于Django的长期运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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