Browse Source

Started on the work of making Celery not dependent on Django.

Ask Solem 16 years ago
parent
commit
b50bd33bdc
2 changed files with 66 additions and 0 deletions
  1. 24 0
      celery/loader/default.py
  2. 42 0
      celery/loader/django.py

+ 24 - 0
celery/loader/default.py

@@ -0,0 +1,24 @@
+
+
+class Loader(object):
+
+    def __init__(self):
+        self._conf_cache = None
+
+    def read_configuration(self):
+        return dict()
+
+    def on_task_preinit(self, task_id, task):
+        pass
+
+    def on_worker_init(self):
+        imports = self.conf.get("imports", [])
+        for module in imports:
+            __import__(module, [], [], {''})
+
+    @property
+    def conf(self):
+        if not self._conf_cache:
+            self._conf_cache = self.read_configuration()
+        return self._conf_cache
+

+ 42 - 0
celery/loader/django.py

@@ -0,0 +1,42 @@
+
+class Loader(object):
+
+    def __init__(self):
+        self._conf_cache = None
+
+    def read_configuration(self):
+        from django.conf import settings
+        return {}
+
+    def on_task_init(self, task_id, task):
+        # See: http://groups.google.com/group/django-users/browse_thread/
+        #       thread/78200863d0c07c6d/38402e76cf3233e8?hl=en&lnk=gst&
+        #       q=multiprocessing#38402e76cf3233e8
+        from django.db import connection
+        connection.close()
+
+        # Reset cache connection only if using memcached/libmemcached
+        from django.core import cache
+        # XXX At Opera we use a custom memcached backend that uses libmemcached
+        # instead of libmemcache (cmemcache). Should find a better solution for
+        # this, but for now "memcached" should probably be unique enough of a
+        # string to not make problems.
+        cache_backend = cache.settings.CACHE_BACKEND
+        if hasattr(cache, "parse_backend_uri"):
+            cache_scheme = cache.parse_backend_uri(cache_backend)[0]
+        else:
+            # Django <= 1.0.2
+            cache_scheme = cache_backend.split(":", 1)[0]
+        if "memcached" in cache_scheme:
+            cache.cache.close()
+
+    def on_worker_init(self):
+        from celery.discovery import autodiscover
+        autodiscover()
+
+    @property
+    def conf(self):
+        if not self._conf_cache:
+            self._conf_cache = self.read_configuration()
+        return self._conf_cache
+