Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
python:celery [2015/06/22 13:17]
vondra [Instalation]
python:celery [2015/06/22 16:08]
vondra [Django Integration]
Line 9: Line 9:
 </​code>​ </​code>​
   - Install Celery <​code>​   - Install Celery <​code>​
-sudo pip install ​Celery+pip install ​celery 
 +pip install django-celery
 </​code>​ </​code>​
 +(note: make sure there is not south installed on your system when using django >= 1.7)
  
 ===== Django Integration ===== ===== Django Integration =====
 http://​celery.readthedocs.org/​en/​latest/​django/​first-steps-with-django.html http://​celery.readthedocs.org/​en/​latest/​django/​first-steps-with-django.html
-  - Create project celery config: <code python>+  - Create project celery config ​projectname/​celery.py: <code python>
 from __future__ import absolute_import from __future__ import absolute_import
  
Line 26: Line 28:
 from django.conf import settings from django.conf import settings
  
-#app = Celery('​celertest') +app = Celery('​celertest',​ broker='​amqp://​guest@localhost//'​)
-app = Celery('​celertest',​ backend='​amqp', broker='​amqp://​guest@localhost//'​) +
  
 # Using a string here means the worker will not have to # Using a string here means the worker will not have to
Line 34: Line 34:
 app.config_from_object('​django.conf:​settings'​) app.config_from_object('​django.conf:​settings'​)
 app.autodiscover_tasks(lambda:​ settings.INSTALLED_APPS) app.autodiscover_tasks(lambda:​ settings.INSTALLED_APPS)
 +
 +app.conf.update(CELERY_RESULT_BACKEND='​djcelery.backends.database.DatabaseBackend'​)
  
  
Line 39: Line 41:
 def debug_task(self):​ def debug_task(self):​
     print('​Request:​ {0!r}'​.format(self.request))     print('​Request:​ {0!r}'​.format(self.request))
 +</​code>​
 +  - Create myapp/​tasks.py<​code python>
 +from __future__ import absolute_import
 +
 +from celery import shared_task
 +
 +
 +@shared_task
 +def add(x, y):
 +    return x + y
 +
 +
 +@shared_task
 +def mul(x, y):
 +    return x * y
 +
 +
 +@shared_task
 +def xsum(numbers):​
 +    return sum(numbers)
 +
 +
 +@shared_task
 +def gen_prime(x):​
 +    multiples = []
 +    results = []
 +    for i in xrange(2, x+1):
 +        if i not in multiples:
 +            results.append(i)
 +            for j in xrange(i*i, x+1, i):
 +                multiples.append(j)
 +    return results
 +</​code>​
 +  - Sync django db (./​manage.py syncdb ​ or ./manage.py makemigrations && migrate)
 +  - Add djcelery to INSTALLED_APPS and add following lines to project/​settings.py:<​code python>
 +CELERYBEAT_SCHEDULER = '​djcelery.schedulers.DatabaseScheduler'​
 +CELERY_RESULT_BACKEND = '​djcelery.backends.database:​DatabaseBackend'​
 +</​code>​
 +  - Run celery watcher: <code bash>​./​manage.py celeryd -E</​code>​
 +  - Optionaly: run celerycam and celerymon to enable the monitoring via DB:<code bash>
 +./manage.py celerycam
 +./manage.py celerymon
 </​code>​ </​code>​
 
python/celery.txt · Last modified: 2015/06/22 16:13 by vondra