Browse Source

Django Loader: New config: CELERY_DB_REUSE_MAX, (max number of times to reuse the db connection)

The default is to use a new connection for every task.
We would very much like to reuse the connection, but a safe number of reuses
is not known, and we don't have any way to handle the errors that might
happen, which may even be database dependent.

See: http://groups.google.com/group/django-users/browse_thread/thread/78200863d0c07c6d/
Ask Solem 15 years ago
parent
commit
d879b4f10a
1 changed files with 12 additions and 3 deletions
  1. 12 3
      celery/loaders/djangoapp.py

+ 12 - 3
celery/loaders/djangoapp.py

@@ -8,12 +8,23 @@ _RACE_PROTECTION = False
 
 class Loader(BaseLoader):
     """The Django loader."""
+    _db_reuse = 0
 
     def read_configuration(self):
         """Load configuration from Django settings."""
         from django.conf import settings
         return settings
 
+    def close_database(self):
+        from django.db import connection
+        db_reuse_max = getattr(self.conf, "CELERY_DB_REUSE_MAX", None)
+        if not db_reuse_max:
+            return connection.close()
+        if self._db_reuse >= db_reuse_max:
+            self._db_reuse = 0
+            return connection.close()
+        self._db_reuse += 1
+
     def on_task_init(self, task_id, task):
         """This method is called before a task is executed.
 
@@ -21,11 +32,9 @@ class Loader(BaseLoader):
         multiprocessing environment.
 
         """
-
         # See http://groups.google.com/group/django-users/
         #            browse_thread/thread/78200863d0c07c6d/
-        from django.db import connection
-        connection.close()
+        self.close_database()
 
         # ## Reset cache connection only if using memcached/libmemcached
         from django.core import cache