MCPcopy Index your code
hub / github.com/plotly/dash / setup_background_callback_app

Function setup_background_callback_app

tests/background_callback/utils.py:77–160  ·  view source on GitHub ↗
(manager_name, app_name)

Source from the content-addressed store, hash-verified

75
76@contextmanager
77def setup_background_callback_app(manager_name, app_name):
78 from dash.testing.application_runners import import_app
79
80 if manager_name == "celery":
81 os.environ["LONG_CALLBACK_MANAGER"] = "celery"
82 redis_url = os.environ["REDIS_URL"].rstrip("/")
83 os.environ["CELERY_BROKER"] = f"{redis_url}/0"
84 os.environ["CELERY_BACKEND"] = f"{redis_url}/1"
85
86 # Clear redis of cached values
87 redis_conn = redis.Redis(host="localhost", port=6379, db=1)
88 cache_keys = redis_conn.keys()
89 if cache_keys:
90 redis_conn.delete(*cache_keys)
91
92 worker = subprocess.Popen(
93 [
94 sys.executable,
95 "-m",
96 "celery",
97 "-A",
98 f"tests.background_callback.{app_name}:handle",
99 "worker",
100 "-P",
101 "prefork",
102 "--concurrency",
103 "2",
104 "--loglevel=info",
105 ],
106 encoding="utf8",
107 preexec_fn=os.setpgrp,
108 stderr=subprocess.PIPE,
109 )
110 # Wait for the worker to be ready, if you cancel before it is ready, the job
111 # will still be queued.
112 lines = []
113 for line in iter(worker.stderr.readline, ""):
114 if "ready" in line:
115 break
116 lines.append(line)
117 else:
118 error = "\n".join(lines)
119 error += f"\nPath: {sys.path}"
120 raise RuntimeError(f"celery failed to start: {error}")
121
122 try:
123 yield import_app(f"tests.background_callback.{app_name}")
124 finally:
125 # Interval may run one more time after settling on final app state
126 # Sleep for 1 interval of time
127 time.sleep(0.5)
128 os.environ.pop("LONG_CALLBACK_MANAGER")
129 os.environ.pop("CELERY_BROKER")
130 os.environ.pop("CELERY_BACKEND")
131 kill(worker.pid)
132 from dash import page_registry
133
134 page_registry.clear()

Calls 8

import_appFunction · 0.90
keysMethod · 0.80
popMethod · 0.80
killFunction · 0.70
deleteMethod · 0.45
appendMethod · 0.45
clearMethod · 0.45
terminate_jobMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…