如何处理aws上的django长时间任务

问题描述:

我是django和aws中的初学者,我试图在aws上运行我的django应用程序,我有这个视图在视频上做了一些处理,并在此过程中显示加载模板运行以使用户等待,所以根据视频大小需要大约3分钟或更长时间,但它在开发模式下工作,但一旦发生故障,过程在最大2700ms后停止。我怎么能在aws上运行这么长的任务?如何处理aws上的django长时间任务

我的观点:

######################### 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 %} 
+0

它是如何部署的,只是一个ec2实例,或者你在使用弹性beanstalk吗? – user602525

+0

是的,我使用弹性豆杆 –

所以这里的问题是,请求/响应周期通常是一终端的响应没有收到,所以你需要什么要做的是触发在后台运行的后台任务,并在完成该过程时向用户发送电子邮件。

我会推荐像芹菜这样的东西,它允许你启动一个过程,并继续中断请求/响应周期。

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

+0

谢谢你的回答,我是否需要定义两个芹菜任务;第一个用于视频处理,另一个用于电子邮件通知,并在我看来延迟打电话给他们? –

+0

@ c.ceti这是解决它的一种方法是。 – myusuf3