Celery任务返回AsyncResult而不是Dictionary

时间:2019-04-09 04:52:29

标签: django rabbitmq django-celery

我正面临问题。我将芹菜用于异步任务。当我运行工作程序(RabbitMQ)时,shared_task成功运行,但未按预期返回结果。我想在shared_task运行时返回字典,但是得到了这种类型的响应。     249ccaf5-dae9-4296-b093-e0f09eff2632

运行shared_task的我的 views.py

def request_twitter(request):
    global screenname
    access_key = request.session['access_key_tw']
    access_token = request.session['access_secret_tw']
    dic = requesting_twitter.delay(access_key,access_token,screenname)
    print(dic)
    return render(request,'info.html', dic)

和我的 tasks.py 文件

@shared_task
    def requesting_twitter(ackey,asecret,sc):
    CONSUMER_KEY = 'K9lD2i0feSrOtCBQVIjvrygce'
    CONSUMER_SECRET = '82HqosHNh4JD4NMruRjRZ4pLU5C1zf983tDzhHlEwuamaSPKGk'
    oauth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    access_key = ackey
    access_secret = asecret
    oauth.set_access_token(access_key, access_secret)
    api = tweepy.API(oauth)
    screenname = sc
    user = api.get_user(screenname)

    userTweet = api.user_timeline(screenname, count=1)
    for tweets in userTweet:
        most_recent_tweet = tweets.created_at
    try:
        sn = user.screen_name
    except:
        sn = ''
    try:
        disply_name = user.name
    except:
        disply_name = ''
    try:
        descriptin = user.description
    except:
        descriptin = ''
    try:
        locatin = user.location
    except:
        locatin = ''
    try:
        urll = user.url
    except:
        urll = ''
    try:
        Date_joined = user.created_at
    except:
        Date_joined = ''
    try:
        twitter_user_id = user.id
    except:
        twitter_user_id = ''
    try:
        profile_lang = user.lang
    except:
        profile_lang = ''
    try:
        time_zzone = user.time_zone
    except:
        time_zzone = ''
    try:
        tweetzz = user.statuses_count
    except:
        tweetzz = ''
    try:
        followingg = user.favourites_count
    except:
        followingg = ''
    try:
        followerss = user.followers_count
    except:
        followerss = ''
    try:
        likess = user.favourites_count
    except:
        likess = ''
    try:
        banner = user.profile_banner_url
    except:
        banner = ''
    try:
        profile_pic = user.profile_image_url
    except:
        profile_pic = ''
    try:
        listss = user.listed_count
    except:
        listss = ''

    dic = {
        'sn': sn,
        'display_name': disply_name,
        'description': descriptin,
        'location': locatin,
        'url': urll,
        'Date_joined': Date_joined,
        'user_id': twitter_user_id,
        'lang': profile_lang,
        'time_zone': time_zzone,
        'tweets': tweetzz,
        'followers': followerss,
        'likes': likess,
        'lists': listss,
        'banner': banner,
        'profile_pic': profile_pic,
        'most_recent_post': most_recent_tweet
    }

    return dic

预先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

顾名思义,它是异步任务。仅在 worker 可用时执行。 如果您需要实时/即时的结果,则不需要异步任务

为此,请调用不带 delay() 部分的函数

def request_twitter(request):
    global screenname
    access_key = request.session['access_key_tw']
    access_token = request.session['access_secret_tw']
    dic = requesting_twitter(access_key,access_token,screenname)
    print(dic)
    return render(request,'info.html', dic)


您可以阅读有关芹菜任务 s
的更多信息 1. Celery: an overview of the architecture and how it works
2. What kinds of things should I use Celery for?