Browse Source

Use Celery to generate tiles

Baptiste Jonglez 10 years ago
parent
commit
a0cbee9bf0
8 changed files with 57 additions and 2 deletions
  1. 5 0
      README.md
  2. 4 0
      panorama/gen_tiles.py
  3. 2 2
      panorama/models.py
  4. 11 0
      panorama/tasks.py
  5. 1 0
      requirements.txt
  6. 5 0
      ztulec/__init__.py
  7. 22 0
      ztulec/celery.py
  8. 7 0
      ztulec/settings.py

+ 5 - 0
README.md

@@ -0,0 +1,5 @@
+
+
+Tile generation uses Celery, to launch a worker, run:
+
+    celery -A ztulec.celery worker --loglevel=info

+ 4 - 0
panorama/gen_tiles.py

@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
 """
 Given an image, generate a set of tiles at various zoom levels.
 The format of the filename of times is:
@@ -8,6 +10,8 @@ where zoom is 0 for the original size, 1 when the image is downscaled 2
 times, 2 when the image is downscaled 4 times, etc.
 """
 
+from __future__ import unicode_literals, division, print_function
+
 import sys
 import os
 import tempfile

+ 2 - 2
panorama/models.py

@@ -11,7 +11,7 @@ from django.core.exceptions import ValidationError
 from django.core.validators import MinValueValidator, MaxValueValidator
 from django.utils.encoding import python_2_unicode_compatible
 
-from .gen_tiles import gen_tiles
+from .tasks import generate_tiles
 
 
 EARTH_RADIUS = 6371009
@@ -135,7 +135,7 @@ class Panorama(ReferencePoint):
             os.makedirs(self.tiles_dir())
         except OSError:
             pass
-        gen_tiles(self.image.path, self.tiles_dir())
+        generate_tiles.delay(self.image.path, self.tiles_dir())
 
     def __str__(self):
         return "Panorama : " + self.name

+ 11 - 0
panorama/tasks.py

@@ -0,0 +1,11 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals, division, print_function, absolute_import
+
+from celery import shared_task
+
+from .gen_tiles import gen_tiles
+
+
+@shared_task
+def generate_tiles(*args, **kwargs):
+    return gen_tiles(*args, **kwargs)

+ 1 - 0
requirements.txt

@@ -1,3 +1,4 @@
 Django==1.7.4
 djangorestframework==3.0.5
 Pillow==2.7.0
+celery==3.1.18

+ 5 - 0
ztulec/__init__.py

@@ -0,0 +1,5 @@
+from __future__ import absolute_import
+
+# This will make sure the app is always imported when
+# Django starts so that shared_task will use this app.
+from .celery import app as celery_app

+ 22 - 0
ztulec/celery.py

@@ -0,0 +1,22 @@
+from __future__ import print_function, unicode_literals, absolute_import
+
+import os
+
+from celery import Celery
+
+# set the default Django settings module for the 'celery' program.
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ztulec.settings')
+
+from django.conf import settings
+
+app = Celery('ztulec')
+
+# Using a string here means the worker will not have to
+# pickle the object when using Windows.
+app.config_from_object('django.conf:settings')
+app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
+
+
+@app.task(bind=True)
+def debug_task(self):
+    print('Request: {0!r}'.format(self.request))

+ 7 - 0
ztulec/settings.py

@@ -36,6 +36,8 @@ INSTALLED_APPS = (
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
+    # For Celery
+    'kombu.transport.django',
     'rest_framework',
     'panorama',
     'api'
@@ -96,3 +98,8 @@ PANORAMA_TILES_DIR = "tiles"
 # Max distance around a point at which to consider reference points
 # (in meters)
 PANORAMA_MAX_DISTANCE = 30000
+
+# Celery configuration
+BROKER_URL = 'django://'
+CELERY_TASK_SERIALIZER = 'json'
+CELERY_ACCEPT_CONTENT = ['json']