celery定时器以及出错解决方案Celery Received unregistered task of type

# coding=utf-8
import connexion
import copy
import requests
import json
import time
import uuid
from celery import Celery
from celery.schedules import crontab
from flask import jsonify
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)

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

application = app.app
# Celery configuration db 7
application.config['CELERY_BROKER_URL'] = 'redis://:pwd@r-xxx.redis.rds.aliyuncs.com:6379/7'
application.config['result_backend'] = 'redis://:pwd@r-xxx.redis.rds.aliyuncs.com:6379/7'

# 调度器
# 问题:Celery Received unregistered task of type
# 解决:我的flask的main.py文件和celery的主脚本task_timed_del_post.py在同一目录,
# 我的定时函数@celery.task(bind=True) def timed_del_post(self)也同时在该脚本中没有独立出去
# 下面的app是我工程项目的目录,在app里开始找,然后找到自己,然后找到函数timed_del_post
# 这里配置调度器和下面使用调度器效果一致
# "schedule": crontab(minute="*/1"),
application.config['beat_schedule'] = {
         "test001": {
            'task': 'app.task_timed_del_post.timed_del_post',
            'schedule': 1.0,
            'args': ()
        }
}


RESULT = {
    'code': 0,
    'message': 'Success',
    'data': {}
}
result = copy.deepcopy(RESULT)


def make_celery(app):
    celery = Celery(app.name, backend=app.config['result_backend'],
                    broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)

    celery.conf.update(
        result_serializer='json',
        task_serializer='json',
    )
    celery.conf.timezone = 'UTC'
    # celery.conf.beat_schedule = {
    #      "test001": {
    #         'task': 'app.task_timed_del_post.timed_del_post',
    #         'schedule': 1.0,
    #         'args': ()
    #     }
    # }

    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 timed_del_post(self):
    print("hello world!")

    return "ok"


# 运行方式:
# 服务器使用一个celery_service
# screen -S celery_service
# cd /root/backend/services/app
# 我的初始化celery对象叫做celery, 如果初始化叫做app,应该默认不写,叫别的名改别的名字
# celery -A app.task_timed_del_post.celery worker -B --loglevel=info
# celery -A app.task_timed_del_post worker -B -s /tmp/celerybeat-schedule --loglevel=info

  

app文件夹下:

task_timed_del_post.py

timed_del_post函数在task_timed_del_post.py

 

更简单点的:

from celery import Celery, Task
from celery.utils.log import get_task_logger
from flask import Flask
from datetime import timedelta

app = Flask(__name__)
app.config['CELERY_NAME'] = 'test_celery'
app.config['CELERY_BROKER_URL'] = 'redis://:lyp82nLF@r-2zeaf096da357c24.redis.rds.aliyuncs.com:6379/7'
app.config['CELERY_RESULT_BACKEND'] = 'redis://:lyp82nLF@r-2zeaf096da357c24.redis.rds.aliyuncs.com:6379/7'
app.config['CELERYBEAT_SCHEDULE'] = {
        'add-every-10-seconds': {
            'task': 'app.task_coretask.long_task',
            'schedule': 1.0,
            'args': ()
        },
    }
logger = get_task_logger(__name__)
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)


@celery.task(bind=True)
def long_task(self):
    """启动worker: celery -A test_celery.celery worker -B --loglevel=debug"""
    logger.debug('================')
    logger.debug('此处写需要定时调用的任务')
    logger.debug('================')
    print("hello world")
    return "ok"

# 服务器使用一个celery_service
# screen -S
# cd /root/backend/services/app
# celery -A app.task_coretask.celery worker -B --loglevel=info

# 测试client
# class Trigger(Task):
#     def run(self):
#         task = long_task.apply_async()
#         print(task)
#
#
# if __name__ == '__main__':
#     trigger = Trigger()
#     trigger.run()

  

 

posted @ 2019-04-11 18:21  Adamanter  阅读(2342)  评论(0编辑  收藏  举报