如何在Django视图中最佳地启动异步请求? [英] How to best launch an asynchronous job request in Django view?

查看:179
本文介绍了如何在Django视图中最佳地启动异步请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个查看功能是一个非常长的处理工作,显然需要以不同的方式处理。
而不是让用户等待很长时间,最好是我可以午餐处理工作,通过电子邮件发送结果,而不用等待完成,通知用户他们的请求正在被处理,让他们浏览。
我知道我可以使用os.fork,但是我想知道Django是否有正确的方式。也许我可以返回HTTP响应,而不是继续这个工作吗?

One of my view functions is a very long processing job and clearly needs to be handled differently. Instead of making the user wait for long time, it would be best if I were able to lunch the processing job which would email the results, and without waiting for completion notify the user that their request is being processed and let them browse on. I know I can use os.fork, but I was wondering if there is a 'right way' in terms of Django. Perhaps I can return the HTTP response, and than go on with this job somehow?

推荐答案

这里有几个解决方案问题,最好的一个取决于你的工作量将会有多沉重。

There are a couple of solutions to this problem, and the best one depends a bit on how heavy your workload will be.

如果你有一个轻量级的工作量,你可以使用 django-mailer ,这是定义作业模型,将新作业保存到数据库中,然后cron经常运行独立脚本来处理存储在数据库中的作业(完成时删除它们)。您可以使用 django-chronograph 等操作更容易地管理作业计划。

If you have a light workload you can use the approach used by django-mailer which is to define a "jobs" model, save new jobs into the database, then have cron run a stand-alone script every so often to process the jobs stored in the database (deleting them when done). You can use something like django-chronograph to manage the job scheduling easier

如果您需要帮助,了解如何编写脚本来处理工作,请参阅James Bennett的文章独立Django脚本,以获得帮助。

If you need help understanding how to write a script to process the job see James Bennett's article Standalone Django Scripts for help.

如果你有一个非常高的工作量,这意味着您不仅需要单个服务器来处理作业,还需要使用真正的分发任务队列。这里有很多的竞争,所以我无法真正详细说明所有选项,但与Django应用程序一起使用的好方法是芹菜

If you have a very high workload, meaning you'll need more than a single server to process the jobs, then you want to use a real distribute task queue. There is a lot of competition here so I can't really detail all the options, but a good one to use with for Django apps is celery.

这篇关于如何在Django视图中最佳地启动异步请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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