【问题标题】:How to handle django long task on aws如何在 aws 上处理 django 长任务
【发布时间】:2017-08-03 19:57:34
【问题描述】:

我是 django 和 aws 的初学者,我正在尝试在 aws 上运行我的 django 应用程序,我有这个视图可以在视频上制作一些进程并在此进程运行时显示加载模板以使用户等待,所以根据视频大小,大约需要 3 分钟或更长时间,它确实在开发模式下工作,但是一旦在 aws 上,该过程在最多 2700 毫秒后停止。我怎么能在 aws 上运行这么长的任务?

我的看法:

######################### Call load template ###############################
def process(request):
    return render(request, 'testgif.html')
######################### Process the video and send notification email to user when process is done ###################################
def getor(request):
    # get video from s3 bucket mounted on ec2 instance
    var = Video.objects.order_by('id').last()
    v = '/mnt/s3/media/videos/' + str(var)
    # process
    subprocess.call("./step1.sh %s" % (str(v)), shell=True)
    #send email notification
    current_site = get_current_site(request)
    user = User.objects.values('username').order_by('id').last()
    us = user['username']
    subject = 'Notification of end of process.'
    message = render_to_string('notify.html', {
        'us':us,
        'domain':current_site.domain,
    })
   eml = User.objects.values('email').order_by('id').last()
   toemail = eml['email']
   email = EmailMessage(subject, message, to=[toemail])
   email.send()
   return render(request, 'endexecut.html')

我的加载模板:

{% extends 'base.html' %}
{% load staticfiles %}

{% block content %}
<div class="container">
    <div class="row">
         <div class="jumbotron">
              <div class="row">
                 <center>
                   <p> Please wait, your video is processing ! </p>
                   <img src="{% static "images/loading1.gif" %}" id="image-id" width="600" height="400" />
                 </center>
               </div>
         </div>
     </div>
 </div>
{% endblock %}
{% block javascript %}
<script type="text/javascript">
  $.ajax({
     url: '/wmark/',
     type: 'GET',
     dataType: 'html',
     success: function(result){
             $('#image-id').attr('src', result.image);
             $('.container').html(result);
     },
     error: function(xhr){
            alert(xhr.responseText); //Remove this when all is fine.
     }
  });
</script>
{% endblock %}

【问题讨论】:

  • 它是如何部署的,只是一个 ec2 实例还是您使用的是弹性 beanstalk?
  • 是的,我正在使用弹性豆茎

标签: django amazon-web-services amazon-ec2


【解决方案1】:

所以这里的问题是,一旦没有收到响应,请求/响应周期通常会终止,因此您需要做的是触发在后台运行的后台任务,并在流程完成时向用户发送电子邮件。

我会推荐像 celery 这样的东西,它允许您启动一个进程并在中断的情况下继续请求/响应周期。

如果您希望在用户等待页面时跟踪某些内容,您还可以查看 Web 套接字,它可以更新您“处理”的进度。但是,一旦 celery 完成视频处理,只需发送一封电子邮件,就可以避免这种额外的工作。

【讨论】:

  • 谢谢你的回答,我是不是要定义两个芹菜任务;第一个用于视频处理,另一个用于电子邮件通知并在我看来延迟打电话给他们?
  • @c.ceti 这是解决它的一种方法。
猜你喜欢
  • 2012-11-26
  • 1970-01-01
  • 2011-01-19
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
相关资源
最近更新 更多