为什么在加载页面时芹菜任务不是asyncing?

时间:2017-10-05 22:30:17

标签: django heroku celery

任务运行但我的页面等待它完成任务然后加载页面。几乎失败了async的目的加上我在heroku上得到了一个timedout - 单独的问题。所以,我在views.py中调用该任务并将其发送到tasks.py。不确定我还需要什么,但逻辑上看起来对我来说?

settings.py

BROKER_URL=['amqp://guest@localhost//','cloudamqp']
BROKER_POOL_LIMIT = 1
BROKER_CONNECTION_MAX_RETRIES = None
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
CELERY_ACCEPT_CONTENT = 'pickle',
CELERY_TASK_SERIALIZER = 'json', 
CELERY_RESULT_SERIALIZER = 'json'
CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend'
CELERY_ALWAYS_EAGER = True

views.py

def my_page(request):
    #do something 

    #this is at the end, right before return. 
    #If I put it in the middle, it runs in sequence. So I don't see anything after this until the task is done.
    get_data.delay(args) 
    return (request, 'home.html')

tasks.py

from __future__ import absolute_import, unicode_literals
import requests
import json
import time
import sys
import os
import random
from os import path
from celery import Celery
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
from lib import myfiles
from django.db import models
from .models import mymodels
import django
django.setup()

@task(name="get_myfunction")
def get_data(user, items):
    #runs a bunch of things
    #saves data
    #all this and page spinner on the browser tab just keeps spinning until its done

1 个答案:

答案 0 :(得分:0)

CELERY_ALWAYS_EAGER = True应为CELERY_ALWAYS_EAGER = False

来自the docs

  

如果这是True,则所有任务将通过阻塞直到本地执行   任务返回。