2017-10-05 97 views
0

任務運行但我的頁面等待它完成任務,然後加載頁面。幾乎打敗了異步的目的,我在heroku上得到了一個超時 - 單獨的問題。所以,我打電話給views.py中的任務並將其發送到tasks.py。不知道我還需要什麼,但在邏輯上看起來很適合我?爲什麼加載頁面時不是芹菜任務異步?

settings.py

BROKER_URL=['amqp://[email protected]//','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 

回答

0

CELERY_ALWAYS_EAGER = True應該CELERY_ALWAYS_EAGER = False

the docs

如果這是真的,所有任務將在本地通過阻塞執行,直到任務返回到 。

+0

ahhh!有道理..謝謝! – JamAndJammies