Przeglądaj źródła

Added celery.app.enable_trace and .disable_trace

Ask Solem 14 lat temu
rodzic
commit
51e65514c0
1 zmienionych plików z 39 dodań i 24 usunięć
  1. 39 24
      celery/app/__init__.py

+ 39 - 24
celery/app/__init__.py

@@ -160,31 +160,46 @@ default_loader = os.environ.get("CELERY_LOADER") or "default"
 #: Global fallback app instance.
 default_app = App(loader=default_loader, set_as_current=False)
 
-if os.environ.get("CELERY_TRACE_APP"):  # pragma: no cover
 
-    def app_or_default(app=None):
-        from traceback import print_stack
-        from multiprocessing import current_process
-        if app is None:
-            if getattr(_tls, "current_app", None):
-                print("-- RETURNING TO CURRENT APP --")
-                print_stack()
-                return _tls.current_app
-            if current_process()._name == "MainProcess":
-                raise Exception("DEFAULT APP")
-            print("-- RETURNING TO DEFAULT APP --")
+def _app_or_default(app=None):
+    """Returns the app provided or the default app if none.
+
+    The environment variable :envvar:`CELERY_TRACE_APP` is used to
+    trace app leaks.  When enabled an exception is raised if there
+    is no active app.
+
+    """
+    if app is None:
+        return getattr(_tls, "current_app", None) or default_app
+    return app
+
+
+def _app_or_default_trace(app=None):  # pragma: no cover
+    from traceback import print_stack
+    from multiprocessing import current_process
+    if app is None:
+        if getattr(_tls, "current_app", None):
+            print("-- RETURNING TO CURRENT APP --")
             print_stack()
-            return default_app
-        return app
-else:
-    def app_or_default(app=None):
-        """Returns the app provided or the default app if none.
+            return _tls.current_app
+        if current_process()._name == "MainProcess":
+            raise Exception("DEFAULT APP")
+        print("-- RETURNING TO DEFAULT APP --")
+        print_stack()
+        return default_app
+    return app
 
-        The environment variable :envvar:`CELERY_TRACE_APP` is used to
-        trace app leaks.  When enabled an exception is raised if there
-        is no active app.
 
-        """
-        if app is None:
-            return getattr(_tls, "current_app", None) or default_app
-        return app
+def enable_trace():
+    global app_or_default
+    app_or_default = _app_or_default_trace
+
+
+def disable_trace():
+    global app_or_default
+    app_or_default = _app_or_default
+
+
+app_or_default = _app_or_default
+if os.environ.get("CELERY_TRACE_APP"):  # pragma: no cover
+    enable_trace()