Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -----------------------------------
- # Mi tasks.py
- # -----------------------------------
- from celery import Celery
- celery = Celery('tasks')
- celery.config_from_object('celeryconfig')
- # Como cada tarea tiene una referencia a su antecesor,
- # hacemos un generador que nos devuelva su antecesor.
- def unpack_chain(nodes):
- while nodes.parent:
- yield nodes.parent
- nodes = nodes.parent
- yield nodes
- @celery.task
- def add(num, num2):
- return num + num2
- # -----------------------------------
- # En ipython:
- # -----------------------------------
- # Hacemos los imports necesarios.
- In [43]: from celery import chain
- In [44]: from tasks import celery, add, unpack_chain
- # Creamos la cola de trabajo, cada tarea add entrega su resultado a la siguiente tarea.
- # El programa sigue ejecutandose porque la cola de trabajo se procesa de manera asíncrona.
- In [45]: c = chain(add.s(3,3), add.s(10).set(countdown=100))
- In [46]: m = c.apply_async()
- # Voy a consultar el estado de esta cadena. Ya tengo el id :)
- In [47]: a = celery.AsyncResult(m.id)
- # Son lo mismo?
- In [48]: a == m
- Out[48]: True
- In [49]: type(m) == type(a) and m.id == a.id
- Out[49]: True
- # Parece que no...
- # obtengo dos listas diferentes.
- In [50]: [t.status for t in list(unpack_chain(a))]
- Out[50]: ['PENDING']
- In [51]: [t.status for t in list(unpack_chain(m))]
- Out[51]: ['PENDING', 'SUCCESS']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement