问题描述
我正在使用celery异步运行任务的Django应用程序。现在,用户可以从网页提交表单以启动芹菜任务。但是,单击Django模板中的按钮无法暂停或停止任务。
到目前为止,这是我的代码
芹菜任务
@shared_task
def get_website(website):
website_list = return_website_list(website)
return website_list
在上面的任务中,我调用一个return_website_list()
函数,该函数将抓取所需的网站并返回该网站的链接列表。
output.html模板
<div class="container">
<button class="pause_btn" type="button">Pause task</button>
<button class="resume_btn" type="button">Resume task</button>
<button class="stop_btn" type="button">Stop task</button>
</div>
我希望单击“暂停”按钮时可以无限期暂停任务,单击“继续”按钮时可以恢复任务,或者单击“停止”按钮时可以完全停止任务。
views.py
def index(request):
if request.method == 'POST':
website = request.POST.get('website-name')
get_website.delay(website)
return redirect('output')
return render(request,'index.html')
我像link1,link2,link3这样在线搜索。但是这些链接并没有帮助我实现我想要做的事情。
预先感谢
解决方法
我的想法是一个名为状态的字段,状态将通过单击暂停按钮进行设置。并在每个动作检查状态的celery任务中,如果它处于暂停状态,请等待1秒钟,然后再次检查。我以前曾将此想法用于取消操作