소스 검색

App.__reduce__ now unpickles by simply using app_or_default, also _current_app is now stored in TLS

Ask Solem 14 년 전
부모
커밋
59c4f5fd47
1개의 변경된 파일9개의 추가작업 그리고 5개의 파일을 삭제
  1. 9 5
      celery/app/__init__.py

+ 9 - 5
celery/app/__init__.py

@@ -9,6 +9,7 @@ Celery Application.
 
 """
 import os
+import threading
 
 from inspect import getargspec
 
@@ -16,10 +17,12 @@ from celery import registry
 from celery.app import base
 from celery.utils.functional import wraps
 
+_tls = threading.local()
+
 # Apps with the :attr:`~celery.app.base.BaseApp.set_as_current` attribute
 # sets this, so it will always contain the last instantiated app,
 # and is the default app returned by :func:`app_or_default`.
-_current_app = None
+_tls.current_app = None
 
 
 class App(base.BaseApp):
@@ -39,8 +42,7 @@ class App(base.BaseApp):
 
     def on_init(self):
         if self.set_as_current:
-            global _current_app
-            _current_app = self
+            _tls.current_app = self
 
     def create_task_cls(self):
         """Creates a base task class using default configuration
@@ -127,6 +129,9 @@ class App(base.BaseApp):
             return inner_create_task_cls()(*args)
         return inner_create_task_cls(**options)
 
+    def __reduce__(self):
+        return (app_or_default, ())
+
 #: The "default" loader is the default loader used by old applications.
 default_loader = os.environ.get("CELERY_LOADER") or "default"
 
@@ -159,7 +164,6 @@ else:
         is no active app.
 
         """
-        global _current_app
         if app is None:
-            return _current_app or default_app
+            return _tls.current_app or default_app
         return app