SHOW:
|
|
- or go back to the newest paste.
1 | # ----------------------------------- | |
2 | # Mi tasks.py | |
3 | # ----------------------------------- | |
4 | ||
5 | from celery import Celery | |
6 | ||
7 | celery = Celery('tasks') | |
8 | celery.config_from_object('celeryconfig') | |
9 | ||
10 | # Como cada tarea tiene una referencia a su antecesor, | |
11 | # hacemos un generador que nos devuelva su antecesor. | |
12 | def unpack_chain(nodes): | |
13 | while nodes.parent: | |
14 | yield nodes.parent | |
15 | nodes = nodes.parent | |
16 | yield nodes | |
17 | ||
18 | @celery.task | |
19 | def add(num, num2): | |
20 | return num + num2 | |
21 | ||
22 | # ----------------------------------- | |
23 | # En ipython: | |
24 | # ----------------------------------- | |
25 | ||
26 | # Hacemos los imports necesarios. | |
27 | In [43]: from celery import chain | |
28 | In [44]: from tasks import celery, add, unpack_chain | |
29 | ||
30 | ||
31 | # Creamos la cola de trabajo, cada tarea add entrega su resultado a la siguiente tarea. | |
32 | # El programa sigue ejecutandose porque la cola de trabajo se procesa de manera asíncrona. | |
33 | In [45]: c = chain(add.s(3,3), add.s(10).set(countdown=100)) | |
34 | In [46]: m = c.apply_async() | |
35 | ||
36 | # Voy a consultar el estado de esta cadena. Ya tengo el id :) | |
37 | In [47]: a = celery.AsyncResult(m.id) | |
38 | ||
39 | # Son lo mismo? | |
40 | In [48]: a == m | |
41 | Out[48]: True | |
42 | In [49]: type(m) == type(a) and m.id == a.id | |
43 | Out[49]: True | |
44 | ||
45 | # Parece que no... | |
46 | # obtengo dos listas diferentes. | |
47 | In [50]: [t.status for t in list(unpack_chain(a))] | |
48 | Out[50]: ['PENDING'] | |
49 | In [51]: [t.status for t in list(unpack_chain(m))] | |
50 | Out[51]: ['PENDING', 'SUCCESS'] |