Skip to content

Instantly share code, notes, and snippets.

@daviddavis
Created July 10, 2017 18:47
Show Gist options
  • Save daviddavis/3b878e15b7a025f967699c68435fcb2b to your computer and use it in GitHub Desktop.
Save daviddavis/3b878e15b7a025f967699c68435fcb2b to your computer and use it in GitHub Desktop.
from celery import Celery, Task
app = Celery('tasks', backend='redis://localhost', broker='pyamqp://guest@localhost//')
@app.task(base=Task, acks_late=True)
def dummy_task():
import time
time.sleep(600) # sleep for five minutes
@daviddavis
Copy link
Author

daviddavis commented Jul 10, 2017

Boot celery with a single worker:

(pulp) [vagrant@pulp2 ~]$ celery multi start worker -A tasks --loglevel=info                                                                                                                                   
celery multi v3.1.20 (Cipater)
> Starting nodes...
        > 1@pulp2.dev: OK
        > worker@pulp2.dev: OK

Queue some tasks up:

(pulp) [vagrant@pulp2 ~]$ python
>>> from tasks import dummy_task
>>> t1 = dummy_task.delay()                                                                                                                                                                                  
>>> t2 = dummy_task.delay()
>>> t3 = dummy_task.delay()

Restart the celery worker:

(pulp) [vagrant@pulp2 ~]$ celery multi restart worker
celery multi v3.1.20 (Cipater)
> celery1@pulp2.dev: DOWN
> Restarting node celery1@pulp2.dev: OK

Now wait 10 min. And back in the python repl:

>>> t1.state
'SUCCESS'
>>> t2.state
'SUCCESS'
>>> t3.state
'SUCCESS'

Confirming the tasks are done:

[vagrant@pulp2 ~]$ rabbitmqadmin list queues
+-----------------------------------------------+----------+
|                     name                      | messages |
+-----------------------------------------------+----------+
| celery                                        | 0        |
| celeryev.2eb0838c-22f3-4b6b-a8f8-742645d387ec | 0        |
| celeryev.7a37b06b-0417-4da3-85db-0a6efee98f53 | 0        |
| celeryev.f6648f53-cec5-41c7-8562-b9db956af87e | 0        |
| worker@pulp2.dev.celery.pidbox                | 0        |
+-----------------------------------------------+----------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment