MCPcopy
hub / github.com/plotly/dash / setup_background_callback_app

Function setup_background_callback_app

tests/async_tests/utils.py:77–155  ·  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 "celery",
95 "-A",
96 f"tests.async_tests.{app_name}:handle",
97 "worker",
98 "-P",
99 "prefork",
100 "--concurrency",
101 "2",
102 "--loglevel=info",
103 ],
104 encoding="utf8",
105 preexec_fn=os.setpgrp,
106 stderr=subprocess.PIPE,
107 )
108 # Wait for the worker to be ready, if you cancel before it is ready, the job
109 # will still be queued.
110 lines = []
111 for line in iter(worker.stderr.readline, ""):
112 if "ready" in line:
113 break
114 lines.append(line)
115 else:
116 error = "\n".join(lines)
117 raise RuntimeError(f"celery failed to start: {error}")
118
119 try:
120 yield import_app(f"tests.async_tests.{app_name}")
121 finally:
122 # Interval may run one more time after settling on final app state
123 # Sleep for 1 interval of time
124 time.sleep(0.5)
125 os.environ.pop("LONG_CALLBACK_MANAGER")
126 os.environ.pop("CELERY_BROKER")
127 os.environ.pop("CELERY_BACKEND")
128 kill(worker.pid)
129 from dash import page_registry
130
131 page_registry.clear()
132
133 elif manager_name == "diskcache":
134 os.environ["LONG_CALLBACK_MANAGER"] = "diskcache"

Callers 2

test_001ab_arbitraryFunction · 0.70
test_002ab_basicFunction · 0.70

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

Tested by 2

test_001ab_arbitraryFunction · 0.56
test_002ab_basicFunction · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…