def index(): print("Start Task ...") result = add.delay(2, 8) print("result:", result) # 存到redis之后,返回的id print("result_id:", result.id) # 存到redis之后,返回的id print("result:", result.get()) # 方法返回值 print("End Task ...") return "celery flask test ok!"
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : weihuchao import datetime if __name__ == '__main__': from task import add print "start", datetime.datetime.now() result = add.delay(2, 3) print result # print result.ready() # True # print result.get() # 5 print "end", datetime.datetime.now()
import time from task import add if __name__ == '__main__': # print(add.delay(4, 4)) result = add.delay(4, 4) no_rt = True while no_rt: if result.ready(): print("result.ready True:" + result) print("result.ready False:" + result) time.sleep(1)
from task import add, cmd # 不是運行add(5, 10) => 這樣只是在本地執行 # 而是要使用 方法名.delay() 這樣才是使用celery分配給worker遠程異步執行 # result = add.delay(5, 10) # 會返回一個對象 # print(result.get()) # t1.get()返回結果(15) # 可以一次給多個任務給多個worker執行 # for i in range(3): # result = add.delay(4, i) # print('第%s個任務: %s+%s=%s' % (i+1, 4, i, result.get())) result = add.delay(5, 10) # 會返回一個對象 while not result.ready(): # result.ready()判斷結果否返回了 print(result.ready()) print(result.get()) # t1.get()返回結果(15)
from task import add,minus taskid = add.delay(4,14) # taskid ='3e65d6b2-ddae-45ad-aaf4-8f1851171770' print taskid # task = add.AsyncResult(taskid) # print task.state,task.get() taskid2 = minus.delay(10,5) print taskid2 # taskid ='99b0d2c5-7dae-4cf3-90ec-962b4db7c323' # task = add.AsyncResult(taskid) # print task.state for i in range(1,100): taskid = add.delay(4,14) # taskid ='3e65d6b2-ddae-45ad-aaf4-8f1851171770' print taskid # task = add.AsyncResult(taskid) # print task.state,task.get() taskid2 = minus.delay(10,5) print taskid2
#!/usr/bin/env python # -*- coding: utf-8 -*- from task import add add.delay(5, 7)
# -*- coding: utf-8 -*- from celery import Celery from task import add import celeryconfig app = Celery(__name__, include=["task"]) # 1.引入配置文件 app.config_from_object(celeryconfig) # 2.直接加载配置 # app.conf.update( # task_serializer='json', # accept_content=['json'], # result_serializer='json', # timezone='Europe/Oslo', # enable_utc=True, # ) if __name__ == '__main__': result = add.delay(30, 42)
def run(): ret = add.delay(5, 9) print('ret: ', ret.get()) print('ready: ', ret.ready())
#!/usr/bin/env python from task import add print add.delay(4, 4)
from task import add, minus taskid = add.delay(4, 14) # taskid ='3e65d6b2-ddae-45ad-aaf4-8f1851171770' print taskid # task = add.AsyncResult(taskid) # print task.state,task.get() taskid2 = minus.delay(10, 5) print taskid2 # taskid ='99b0d2c5-7dae-4cf3-90ec-962b4db7c323' # task = add.AsyncResult(taskid) # print task.state for i in range(1, 100): taskid = add.delay(4, 14) # taskid ='3e65d6b2-ddae-45ad-aaf4-8f1851171770' print taskid # task = add.AsyncResult(taskid) # print task.state,task.get() taskid2 = minus.delay(10, 5) print taskid2
import time from celery.result import AsyncResult from task import add result = add.delay(1, 2) while True: promise = AsyncResult(result.task_id) status = promise.status print(status) if 'SUCCESS' in status: print(f'result after 5 seconds wait {result.get()}') break if 'FAILURE' in status: print('Wrong happens', promise) break time.sleep(5)
from task import add from ces.task import add2 if __name__ == '__main__': print('Start task...') resutl = add.delay(2, 3) print(resutl.id) # 获取任务id print(resutl.get()) # 获取任务执行结果 print('End task...')