Browse Source

Adds app.close to close the pool, also 'with Celery() as celery'

Ask Solem 12 years ago
parent
commit
be72052138
1 changed files with 12 additions and 0 deletions
  1. 12 0
      celery/app/base.py

+ 12 - 0
celery/app/base.py

@@ -99,6 +99,15 @@ class Celery(object):
     def set_current(self):
         _tls.current_app = self
 
+    def __enter__(self):
+        return self
+
+    def __exit__(self, *exc_info):
+        self.close()
+
+    def close(self):
+        self._maybe_close_pool()
+
     def on_init(self):
         """Optional callback called at init."""
         pass
@@ -321,6 +330,9 @@ class Celery(object):
         return s
 
     def _after_fork(self, obj_):
+        self._maybe_close_pool()
+
+    def _maybe_close_pool(self):
         if self._pool:
             self._pool.force_close_all()
             self._pool = None