作业在 Celery 终端完成,但 `task.ready()` returns false (Django)

Job completing in Celery terminal but `task.ready()` returns false (Django)

我在芹菜中有以下任务:

@shared_task
def convert_to(audio_file_pk):
     file_obj = AudioFile.objects.get(pk=audio_file_pk)
     print("Tsk Run: Returning {}".format(file_obj.audio_file.path))
     return(file_obj.audio_file.path)

我上传文件的主页视图中有以下内容:

def model_form_upload(request, pk=None):
    if request.method == 'POST':
        form = AudioForm(request.POST, request.FILES)
        if form.is_valid():
            obj = form.save()
            task = convert_to.apply_async(args=[obj.pk])
            return render(request, 'index.html', {
            'form': form, 'task_id' : task.task_id
            })
            #return redirect('home', args=[obj.pk])
    else:
        form = AudioForm()
        return render(request, 'index.html', {
        'form': form
        })

我的 Ajax 视图中有以下代码:

def ajax_view(request, task_id):
    results = convert_to.AsyncResult(task_id)
    print(results.ready())
    print(task_id)
    if results.ready():
        return render_to_response('download_ready.html',
                                  {'results': results.get()})
    return render_to_response('not_ready.html', {})

这是我的 urls.py

url(r'^result/(?P<task_id>.*)$', views.ajax_view, name='result')

这是我在 celery 中的任务结果:

[2017-12-16 09:08:38,534: INFO/MainProcess] Received task: nightcore.tasks.convert_to_nightcore[cd1b19fd-f721-4fa1-b9db-1ce01738d030]  
[2017-12-16 09:08:38,536: INFO/ForkPoolWorker-2] Task nightcore.tasks.convert_to_nightcore[cd1b19fd-f721-4fa1-b9db-1ce01738d030] succeeded in 0.00173856299989s: '/home/student/PycharmProjects/tonightcore/media/uploads/The_Skeptic_in_the_Room-OPs_j1EEplI_3O4k7JT.mp3'

任务打印出它应该打印的值。

print(results.ready()) 打印出 False

这是 print(task_id) 在我的 Ajax 视图中的打印输出值:

[16/Dec/2017 09:11:19] "GET /result/cd1b19fd-f721-4fa1-b9db-1ce01738d030 HTTP/1.1" 200 22
cd1b19fd-f721-4fa1-b9db-1ce01738d030

如您所见,值是相同的,但是当我查询 http://127.0.0.1:8000/result/cd1b19fd-f721-4fa1-b9db-1ce01738d030 时,它 return 是 not_ready.html 而它应该是 return download_ready.html

似乎有很多其他人在 SO 上面临这个问题,但没有任何答案或解决方案。

我在这里找到了解决方案。

https://github.com/celery/celery/issues/3675

我卸载了librabbitmq

$ # call this command many times, until it says it's not installed
$ pip uninstall librabbitmq

然后将经纪人更改为pyamqp

CELERY_BROKER_URL = 'pyamqp://localhost'

已修复。现在我可以得到结果了。