如何最好地开展在Django视图中的异步作业请求? [英] How to best launch an asynchronous job request in Django view?

查看:301
本文介绍了如何最好地开展在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的邮件中使用的方法这是定义一个工作模式,保存新的就业机会到数据库中,然后让cron运行一个独立脚本,每隔一段时间来处理存储在数据库中的作业(删除它们完成时)。您可以使用像 Django的计时码表来管理作业调度更容易

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

如果您需要帮助了解如何编写一个脚本来处理作业看詹姆斯·贝内特的文章的独立的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天全站免登陆