celery 基础

zzy_celery

# 1. app 任务 创建或者发布任务
# 2. broker backend
# 3.Worker

from celery import Celery
import time
my_task = Celery('task', broker='redis://39.105.68.23',
                 backend='redis://39.105.168.23:6379')


@my_task.task
def my_func1(a, b):
    time.sleep(1)
    return f'my_func1 return{a}{b}'


@my_task.task
def ny_func2():
    return 'my_func2'


@my_task.task
def my_func3():
    return 'my_func3'


from .s1 import my_func1

res = my_func1.delay(2, 3)
print(res)



from celery.result import AsyncResult
from .s1 import my_task

# 异步获取任务返回值
async_task = AsyncResult(id='8b45b258-aaaf-433f-8f2d-76d913abecdb',app=my_task)
result = async_task.get()
print(result)

# 判断异步任务是否执行成功
# if async_task.successful():
#   # 获取异步任务的返回值
#   result = async_task.get()
#   print(result)
# else:
#       print("任务还未执行成功")



# 1. app 任务 创建或者发布任务
# 2. broker backend
# 3. Worker

from celery import Celery
my_task = Celery("task", broker="redis://127.0.0.1:6379", backend="redis://127.0.0.1:6379")


@my_task.task
def my_func1(a, b):
    return f"my_func1 return{a}{b}"


@my_task.task
def my_func2():
    return "my_func2"


@my_task.task
def my_func3():
    return "my_func3"



timed_task

import datetime
import time

from .s4 import my_func1

tp = time.time()
utc_time = datetime.datetime.utcfromtimestamp(tp)
add_time = datetime.timedelta(seconds=10)
utc_time = utc_time + add_time

res = my_func1.apply_async(args=(7, 8), eta=utc_time)


celery.py

from celery import Celery
my_task = Celery("task",
                 broker="redis://127.0.0.1:6379",
                 backend="redis://127.0.0.1:6379",
                 include=["Celery_task.task_one", "Celery_task.task_two"])

task_one.py

from celery_task.celery import my_task


@my_task.task
def func1():
    return 123


@my_task.task
def func3():
    return 789

task_two.py

from celery_task.celery import my_task


@my_task.task
def func2():
    return 456

posted @ 2019-02-18 18:42  玩蛇少年7372  阅读(163)  评论(1编辑  收藏  举报