├── requirements.txt ├── images ├── 1.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg └── 5.jpg ├── celeryconfig.py ├── constants.py ├── README.md ├── demo.py ├── Vagrantfile ├── tasks.py └── .gitignore /requirements.txt: -------------------------------------------------------------------------------- 1 | celery==3.1.17 2 | PIL==1.1.7 -------------------------------------------------------------------------------- /images/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erdem/celery-example/HEAD/images/1.jpg -------------------------------------------------------------------------------- /images/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erdem/celery-example/HEAD/images/2.jpg -------------------------------------------------------------------------------- /images/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erdem/celery-example/HEAD/images/3.jpg -------------------------------------------------------------------------------- /images/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erdem/celery-example/HEAD/images/4.jpg -------------------------------------------------------------------------------- /images/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/erdem/celery-example/HEAD/images/5.jpg -------------------------------------------------------------------------------- /celeryconfig.py: -------------------------------------------------------------------------------- 1 | BROKER_URL = 'amqp://celeryuser:celery@localhost:5672/celeryvhost' 2 | 3 | CELERY_RESULT_BACKEND = 'redis://localhost:6379/0' 4 | 5 | CELERY_IMPORTS = ('tasks', ) -------------------------------------------------------------------------------- /constants.py: -------------------------------------------------------------------------------- 1 | THUMBNAILS_DIRECTORY = 'thumbnails/' 2 | IMAGE_DIRECTORY = 'images/' 3 | IMAGE_EXTENSIONS = (".png", ".jpg", ".jpeg") 4 | THUMBNAIL_SIZES = ( 5 | (500, 500), # large 6 | (300, 300), # medium 7 | (120, 120), # small 8 | ) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Celery and Rabbitmq example ## 2 | 3 | Basic example that shows how to interact with celery and rabbitmq to process jobs. 4 | 5 | Find the the blog post here: [http://erdemozkol.com/python-celery.html](http://erdemozkol.com/python-celery.html) 6 | 7 | -------------------------------------------------------------------------------- /demo.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from tasks import thumbnail_generator 4 | from constants import IMAGE_DIRECTORY, IMAGE_EXTENSIONS 5 | 6 | 7 | def generate_thumbnails(): 8 | files = os.listdir(IMAGE_DIRECTORY) 9 | for file in files: 10 | file_name, file_extension = os.path.splitext(file) 11 | if file_extension in IMAGE_EXTENSIONS: 12 | image_path = os.path.join(IMAGE_DIRECTORY, file_name) 13 | thumbnail_generator.delay(image_path, file_name) 14 | 15 | if __name__ == '__main__': 16 | generate_thumbnails() 17 | 18 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | Vagrant.configure("2") do |config| 5 | config.vm.box = "blogto" 6 | config.vm.box_url = "http://files.vagrantup.com/precise64.box" 7 | 8 | config.vm.network :forwarded_port, guest: 80, host: 8080 9 | config.vm.network :forwarded_port, guest: 8080, host: 8081 10 | config.vm.network :forwarded_port, guest: 83, host: 8083 11 | config.vm.network :private_network, ip: "33.33.33.10" 12 | config.vm.synced_folder ".", "/celery_example", :nfs => true 13 | 14 | config.vm.provider :virtualbox do |vb| 15 | vb.customize ["modifyvm", :id, "--memory", "1024"] 16 | vb.customize ["modifyvm", :id, "--rtcuseutc", "on"] 17 | end 18 | 19 | 20 | 21 | end 22 | -------------------------------------------------------------------------------- /tasks.py: -------------------------------------------------------------------------------- 1 | import os 2 | import Image 3 | 4 | from celery.task import task 5 | from constants import THUMBNAIL_SIZES, THUMBNAILS_DIRECTORY 6 | 7 | # Every celery task function must be annotated with a celery task decorator. 8 | # "@task" decorator registered function to celery 9 | 10 | 11 | @task(name="thumbnail_generator") 12 | def thumbnail_generator(image_path, image_name): 13 | for thumbnail_size in THUMBNAIL_SIZES: 14 | thumbnail_folder_name = "%sx%s/" % thumbnail_size 15 | thumbnail_directory = THUMBNAILS_DIRECTORY + thumbnail_folder_name 16 | 17 | thumbnail_data = { 18 | "image_name": os.path.splitext(image_name)[0], 19 | "width": thumbnail_size[0], 20 | "height": thumbnail_size[1] 21 | } 22 | thumbnail_name = "%(image_name)s_%(width)sx%(height)s.jpg" % thumbnail_data 23 | thumbnail_path = thumbnail_directory + thumbnail_name 24 | 25 | image = Image.open(image_path) 26 | image.thumbnail(thumbnail_size) 27 | image.save(thumbnail_path, "JPEG") 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Python template 2 | # Byte-compiled / optimized / DLL files 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | env/ 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *,cover 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | # Created by .ignore support plugin (hsz.mobi) 62 | --------------------------------------------------------------------------------