celery异步任务

这里是celery实现异步的方法

 task.py  你可以叫任何名字,但是celery命令的时候要同时改

celery -A package_dir.your_task_filename.your_celery_app_name worker -l info
# coding=utf-8
import connexion
import copy
import requests
import json
import time
import uuid
from celery import Celery
from flask import jsonify
from bson.objectid import ObjectId
from .api.endpoints.config import CELERY_BROKER_URL_TASK, CELERY_RESULT_BACKEND_TASK

app = connexion.FlaskApp(__name__, specification_dir='.')


application = app.app
# Celery configuration db 9
application.config['CELERY_BROKER_URL'] = CELERY_BROKER_URL_TASK
application.config['CELERY_RESULT_BACKEND'] = CELERY_RESULT_BACKEND_TASK


def make_celery(app):
    celery = Celery(app.name, backend=app.config['CELERY_RESULT_BACKEND'],
                    broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    TaskBase = celery.Task
    class ContextTask(TaskBase):
        abstract = True
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)
    celery.Task = ContextTask
    return celery

celery = make_celery(app.app)

@celery.task(bind=True)
def calling_api(self, url):
    return url


#
# 服务器使用一个
# screen -S celery_service
# cd app, app和flask的main.py同一目录,都位于文件夹app下,我的celery叫做celery,不叫app;
# celery -A app.task.celery worker -l info

  

在别的地方使用的时候, 如果不需要返回结果:

from ...task import calling_api
async_outcome = calling_api.apply_async(args=[url])或者
async_outcome = calling_api.delay(args=[url])

 

在别的地方使用的时候, 如果需要返回结果:

async_outcome = calling_api.apply_async(args=[url])
task_id = str(async_outcome)
task = calling_api.AsyncResult(task_id)
start = time.time()
while True:
    if time.time() - start > 10:
        result['code'] = 1
        result['message'] = "failed, time out!"
        break
    elif task.status == 'SUCCESS':
        print("ASYNC task_id:", task.id)
        print("ASYNC task:", task.result)
        result['data']['image'] = task.result.get("data").get("image")
        break

  

 

posted @ 2019-04-11 19:59  Adamanter  阅读(255)  评论(0编辑  收藏  举报