Skip to content

Commit d0c4c3c

Browse files
committed
rename most long -> background callback
1 parent 963b17f commit d0c4c3c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+247
-274
lines changed

dash/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
get_relative_path,
2727
strip_relative_path,
2828
)
29-
from .long_callback import ( # noqa: F401,E402
29+
from .background_callback import ( # noqa: F401,E402
3030
CeleryManager,
3131
DiskcacheManager,
3232
)

dash/_callback.py

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
PreventUpdate,
2121
WildcardInLongCallback,
2222
MissingLongCallbackManagerError,
23-
LongCallbackError,
23+
BackgroundCallbackError,
2424
ImportedInsideCallbackError,
2525
)
2626

@@ -39,7 +39,7 @@
3939
)
4040

4141
from . import _validate
42-
from .long_callback.managers import BaseLongCallbackManager
42+
from .background_callback.managers import BaseBackgroundCallbackManager
4343
from ._callback_context import context_value
4444

4545

@@ -72,7 +72,7 @@ def callback(
7272
progress_default: Any = None,
7373
running: Optional[List[Tuple[Output, Any, Any]]] = None,
7474
cancel: Optional[List[Input]] = None,
75-
manager: Optional[BaseLongCallbackManager] = None,
75+
manager: Optional[BaseBackgroundCallbackManager] = None,
7676
cache_args_to_ignore: Optional[list] = None,
7777
on_error: Optional[Callable[[Exception], Any]] = None,
7878
**_kwargs,
@@ -96,11 +96,11 @@ def callback(
9696
9797
:Keyword Arguments:
9898
:param background:
99-
Mark the callback as a long callback to execute in a manager for
99+
Mark the callback as a background callback to execute in a manager for
100100
callbacks that take a long time without locking up the Dash app
101101
or timing out.
102102
:param manager:
103-
A long callback manager instance. Currently, an instance of one of
103+
A background callback manager instance. Currently, an instance of one of
104104
`DiskcacheManager` or `CeleryManager`.
105105
Defaults to the `background_callback_manager` instance provided to the
106106
`dash.Dash constructor`.
@@ -144,14 +144,14 @@ def callback(
144144
this should be a list of argument names as strings. Otherwise,
145145
this should be a list of argument indices as integers.
146146
:param interval:
147-
Time to wait between the long callback update requests.
147+
Time to wait between the background callback update requests.
148148
:param on_error:
149149
Function to call when the callback raises an exception. Receives the
150150
exception object as first argument. The callback_context can be used
151151
to access the original callback inputs, states and output.
152152
"""
153153

154-
long_spec = None
154+
background_spec = None
155155

156156
config_prevent_initial_callbacks = _kwargs.pop(
157157
"config_prevent_initial_callbacks", False
@@ -160,54 +160,56 @@ def callback(
160160
callback_list = _kwargs.pop("callback_list", GLOBAL_CALLBACK_LIST)
161161

162162
if background:
163-
long_spec: Any = {
163+
background_spec: Any = {
164164
"interval": interval,
165165
}
166166

167167
if manager:
168-
long_spec["manager"] = manager
168+
background_spec["manager"] = manager
169169

170170
if progress:
171-
long_spec["progress"] = coerce_to_list(progress)
172-
validate_long_inputs(long_spec["progress"])
171+
background_spec["progress"] = coerce_to_list(progress)
172+
validate_background_inputs(background_spec["progress"])
173173

174174
if progress_default:
175-
long_spec["progressDefault"] = coerce_to_list(progress_default)
175+
background_spec["progressDefault"] = coerce_to_list(progress_default)
176176

177-
if not len(long_spec["progress"]) == len(long_spec["progressDefault"]):
177+
if not len(background_spec["progress"]) == len(
178+
background_spec["progressDefault"]
179+
):
178180
raise Exception(
179181
"Progress and progress default needs to be of same length"
180182
)
181183

182184
if cancel:
183185
cancel_inputs = coerce_to_list(cancel)
184-
validate_long_inputs(cancel_inputs)
186+
validate_background_inputs(cancel_inputs)
185187

186-
long_spec["cancel"] = [c.to_dict() for c in cancel_inputs]
187-
long_spec["cancel_inputs"] = cancel_inputs
188+
background_spec["cancel"] = [c.to_dict() for c in cancel_inputs]
189+
background_spec["cancel_inputs"] = cancel_inputs
188190

189191
if cache_args_to_ignore:
190-
long_spec["cache_args_to_ignore"] = cache_args_to_ignore
192+
background_spec["cache_args_to_ignore"] = cache_args_to_ignore
191193

192194
return register_callback(
193195
callback_list,
194196
callback_map,
195197
config_prevent_initial_callbacks,
196198
*_args,
197199
**_kwargs,
198-
long=long_spec,
200+
long=background_spec,
199201
manager=manager,
200202
running=running,
201203
on_error=on_error,
202204
)
203205

204206

205-
def validate_long_inputs(deps):
207+
def validate_background_inputs(deps):
206208
for dep in deps:
207209
if dep.has_wildcard():
208210
raise WildcardInLongCallback(
209211
f"""
210-
long callbacks does not support dependencies with
212+
background callbacks does not support dependencies with
211213
pattern-matching ids
212214
Received: {repr(dep)}\n"""
213215
)
@@ -318,7 +320,7 @@ def register_callback(
318320
multi = True
319321
has_output = len(output) > 0
320322

321-
long = _kwargs.get("long")
323+
background = _kwargs.get("long")
322324
manager = _kwargs.get("manager")
323325
running = _kwargs.get("running")
324326
on_error = _kwargs.get("on_error")
@@ -342,7 +344,7 @@ def register_callback(
342344
flat_state,
343345
inputs_state_indices,
344346
prevent_initial_call,
345-
long=long,
347+
long=background,
346348
manager=manager,
347349
dynamic_creator=allow_dynamic_callbacks,
348350
running=running,
@@ -352,23 +354,25 @@ def register_callback(
352354
# pylint: disable=too-many-locals
353355
def wrap_func(func):
354356

355-
if long is not None:
356-
long_key = BaseLongCallbackManager.register_func(
357+
if background is not None:
358+
background_key = BaseBackgroundCallbackManager.register_func(
357359
func,
358-
long.get("progress") is not None,
360+
background.get("progress") is not None,
359361
callback_id,
360362
)
361363

362364
@wraps(func)
363365
def add_context(*args, **kwargs):
364366
output_spec = kwargs.pop("outputs_list")
365-
app_callback_manager = kwargs.pop("long_callback_manager", None)
367+
app_callback_manager = kwargs.pop("background_callback_manager", None)
366368

367369
callback_ctx = kwargs.pop(
368370
"callback_context", AttributeDict({"updated_props": {}})
369371
)
370372
app = kwargs.pop("app", None)
371-
callback_manager = long and long.get("manager", app_callback_manager)
373+
callback_manager = background and background.get(
374+
"manager", app_callback_manager
375+
)
372376
error_handler = on_error or kwargs.pop("app_on_error", None)
373377
original_packages = set(ComponentRegistry.registry)
374378

@@ -384,18 +388,18 @@ def add_context(*args, **kwargs):
384388
response: dict = {"multi": True}
385389
has_update = False
386390

387-
if long is not None:
391+
if background is not None:
388392
if not callback_manager:
389393
raise MissingLongCallbackManagerError(
390-
"Running `long` callbacks requires a manager to be installed.\n"
394+
"Running `background` callbacks requires a manager to be installed.\n"
391395
"Available managers:\n"
392396
"- Diskcache (`pip install dash[diskcache]`) to run callbacks in a separate Process"
393397
" and store results on the local filesystem.\n"
394398
"- Celery (`pip install dash[celery]`) to run callbacks in a celery worker"
395399
" and store results on redis.\n"
396400
)
397401

398-
progress_outputs = long.get("progress")
402+
progress_outputs = background.get("progress")
399403
cache_key = flask.request.args.get("cacheKey")
400404
job_id = flask.request.args.get("job")
401405
old_job = flask.request.args.getlist("oldJob")
@@ -404,7 +408,7 @@ def add_context(*args, **kwargs):
404408
func,
405409
# Inputs provided as dict is kwargs.
406410
func_args if func_args else func_kwargs,
407-
long.get("cache_args_to_ignore", []),
411+
background.get("cache_args_to_ignore", []),
408412
)
409413

410414
if old_job:
@@ -414,7 +418,7 @@ def add_context(*args, **kwargs):
414418
if not cache_key:
415419
cache_key = current_key
416420

417-
job_fn = callback_manager.func_registry.get(long_key)
421+
job_fn = callback_manager.func_registry.get(background_key)
418422

419423
ctx_value = AttributeDict(**context_value.get())
420424
ctx_value.ignore_register_page = True
@@ -433,11 +437,11 @@ def add_context(*args, **kwargs):
433437
"job": job,
434438
}
435439

436-
cancel = long.get("cancel")
440+
cancel = background.get("cancel")
437441
if cancel:
438442
data["cancel"] = cancel
439443

440-
progress_default = long.get("progressDefault")
444+
progress_default = background.get("progressDefault")
441445
if progress_default:
442446
data["progressDefault"] = {
443447
str(o): x
@@ -461,11 +465,11 @@ def add_context(*args, **kwargs):
461465

462466
elif (
463467
isinstance(output_value, dict)
464-
and "long_callback_error" in output_value
468+
and "background_callback_error" in output_value
465469
):
466-
error = output_value.get("long_callback_error", {})
467-
exc = LongCallbackError(
468-
f"An error occurred inside a long callback: {error['msg']}\n{error['tb']}"
470+
error = output_value.get("background_callback_error", {})
471+
exc = BackgroundCallbackError(
472+
f"An error occurred inside a background callback: {error['msg']}\n{error['tb']}"
469473
)
470474
if error_handler:
471475
output_value = error_handler(exc)
@@ -555,7 +559,7 @@ def add_context(*args, **kwargs):
555559
output_value = []
556560
flat_output_values = []
557561

558-
if not long:
562+
if not background:
559563
has_update = _set_side_update(callback_ctx, response) or has_update
560564

561565
if not has_update:

dash/_validate.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ def check_obsolete(kwargs):
364364
file=sys.stderr,
365365
)
366366
continue
367+
if key in ["long_callback_manager"]:
368+
raise exceptions.ObsoleteKwargException(
369+
"long_callback_manager is obsolete, use background_callback_manager instead"
370+
)
367371
# any other kwarg mimic the built-in exception
368372
raise TypeError(f"Dash() got an unexpected keyword argument '{key}'")
369373

@@ -522,9 +526,9 @@ def validate_module_name(module):
522526
return module
523527

524528

525-
def validate_long_callbacks(callback_map):
526-
# Validate that long callback side output & inputs are not circular
527-
# If circular, triggering a long callback would result in a fatal server/computer crash.
529+
def validate_background_callbacks(callback_map):
530+
# Validate that background callback side output & inputs are not circular
531+
# If circular, triggering a background callback would result in a fatal server/computer crash.
528532
all_outputs = set()
529533
input_indexed = {}
530534
for callback in callback_map.values():
@@ -535,21 +539,21 @@ def validate_long_callbacks(callback_map):
535539
input_indexed[o].update(coerce_to_list(callback["raw_inputs"]))
536540

537541
for callback in (x for x in callback_map.values() if x.get("long")):
538-
long_info = callback["long"]
539-
progress = long_info.get("progress", [])
540-
running = long_info.get("running", [])
542+
bg_info = callback["long"]
543+
progress = bg_info.get("progress", [])
544+
running = bg_info.get("running", [])
541545

542-
long_inputs = coerce_to_list(callback["raw_inputs"])
546+
bg_inputs = coerce_to_list(callback["raw_inputs"])
543547
outputs = set([x[0] for x in running] + progress)
544548
circular = [
545549
x
546550
for x in set(k for k, v in input_indexed.items() if v.intersection(outputs))
547-
if x in long_inputs
551+
if x in bg_inputs
548552
]
549553

550554
if circular:
551-
raise exceptions.LongCallbackError(
552-
f"Long callback circular error!\n{circular} is used as input for a long callback"
555+
raise exceptions.BackgroundCallbackError(
556+
f"Background callback circular error!\n{circular} is used as input for a background callback"
553557
f" but also used as output from an input that is updated with progress or running argument."
554558
)
555559

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from .managers.celery_manager import ( # noqa: F401,E402
2-
CeleryLongCallbackManager,
32
CeleryManager,
43
)
54
from .managers.diskcache_manager import ( # noqa: F401,E402
6-
DiskcacheLongCallbackManager,
75
DiskcacheManager,
86
)

dash/long_callback/managers/__init__.py renamed to dash/background_callback/managers/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import hashlib
44

55

6-
class BaseLongCallbackManager(ABC):
6+
class BaseBackgroundCallbackManager(ABC):
77
UNDEFINED = object()
88

99
# Keep a ref to all the ref to register every callback to every manager.
@@ -18,7 +18,7 @@ def __init__(self, cache_by):
1818

1919
self.cache_by = cache_by
2020

21-
BaseLongCallbackManager.managers.append(self)
21+
BaseBackgroundCallbackManager.managers.append(self)
2222

2323
self.func_registry = {}
2424

@@ -83,16 +83,16 @@ def register(self, key, fn, progress):
8383

8484
@staticmethod
8585
def register_func(fn, progress, callback_id):
86-
key = BaseLongCallbackManager.hash_function(fn, callback_id)
87-
BaseLongCallbackManager.functions.append(
86+
key = BaseBackgroundCallbackManager.hash_function(fn, callback_id)
87+
BaseBackgroundCallbackManager.functions.append(
8888
(
8989
key,
9090
fn,
9191
progress,
9292
)
9393
)
9494

95-
for manager in BaseLongCallbackManager.managers:
95+
for manager in BaseBackgroundCallbackManager.managers:
9696
manager.register(key, fn, progress)
9797

9898
return key

dash/long_callback/managers/celery_manager.py renamed to dash/background_callback/managers/celery_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
from dash._callback_context import context_value
88
from dash._utils import AttributeDict
99
from dash.exceptions import PreventUpdate
10-
from dash.long_callback._proxy_set_props import ProxySetProps
11-
from dash.long_callback.managers import BaseLongCallbackManager
10+
from dash.background_callback._proxy_set_props import ProxySetProps
11+
from dash.background_callback.managers import BaseBackgroundCallbackManager
1212

1313

14-
class CeleryManager(BaseLongCallbackManager):
14+
class CeleryManager(BaseBackgroundCallbackManager):
1515
"""Manage background execution of callbacks with a celery queue."""
1616

1717
def __init__(self, celery_app, cache_by=None, expire=None):
@@ -138,7 +138,7 @@ def get_updated_props(self, key):
138138
def _make_job_fn(fn, celery_app, progress, key):
139139
cache = celery_app.backend
140140

141-
@celery_app.task(name=f"long_callback_{key}")
141+
@celery_app.task(name=f"background_callback_{key}")
142142
def job_fn(result_key, progress_key, user_callback_args, context=None):
143143
def _set_progress(progress_value):
144144
if not isinstance(progress_value, (list, tuple)):
@@ -184,7 +184,7 @@ def run():
184184
result_key,
185185
json.dumps(
186186
{
187-
"long_callback_error": {
187+
"background_callback_error": {
188188
"msg": str(err),
189189
"tb": traceback.format_exc(),
190190
}

0 commit comments

Comments
 (0)