@@ -379,21 +379,10 @@ def initialize_context(args, kwargs, inputs_state_indices):
379
379
False ,
380
380
)
381
381
382
- def handle_long_callback (
383
- kwargs ,
384
- long ,
385
- long_key ,
386
- callback_ctx ,
387
- response ,
388
- error_handler ,
389
- func ,
390
- func_args ,
391
- func_kwargs ,
392
- has_update = False ,
393
- ):
382
+ def get_callback_manager (kwargs ):
394
383
"""Set up the long callback and manage jobs."""
395
384
callback_manager = long .get (
396
- "manager" , kwargs .pop ("long_callback_manager" , None )
385
+ "manager" , kwargs .get ("long_callback_manager" , None )
397
386
)
398
387
if not callback_manager :
399
388
raise MissingLongCallbackManagerError (
@@ -405,63 +394,120 @@ def handle_long_callback(
405
394
" and store results on redis.\n "
406
395
)
407
396
408
- progress_outputs = long .get ("progress" )
409
- cache_key = flask .request .args .get ("cacheKey" )
410
- job_id = flask .request .args .get ("job" )
411
397
old_job = flask .request .args .getlist ("oldJob" )
412
398
413
- current_key = callback_manager .build_cache_key (
399
+ if old_job :
400
+ for job in old_job :
401
+ callback_manager .terminate_job (job )
402
+
403
+ return callback_manager
404
+
405
+ def setup_long_callback (
406
+ kwargs ,
407
+ long ,
408
+ long_key ,
409
+ func ,
410
+ func_args ,
411
+ func_kwargs ,
412
+ ):
413
+ """Set up the long callback and manage jobs."""
414
+ callback_manager = get_callback_manager (kwargs )
415
+
416
+ progress_outputs = long .get ("progress" )
417
+
418
+ cache_key = callback_manager .build_cache_key (
414
419
func ,
415
420
# Inputs provided as dict is kwargs.
416
421
func_args if func_args else func_kwargs ,
417
422
long .get ("cache_args_to_ignore" , []),
418
423
)
419
424
420
- if old_job :
421
- for job in old_job :
422
- callback_manager .terminate_job (job )
425
+ job_fn = callback_manager .func_registry .get (long_key )
423
426
424
- if not cache_key :
425
- cache_key = current_key
427
+ ctx_value = AttributeDict (** context_value .get ())
428
+ ctx_value .ignore_register_page = True
429
+ ctx_value .pop ("background_callback_manager" )
430
+ ctx_value .pop ("dash_response" )
426
431
427
- job_fn = callback_manager .func_registry .get (long_key )
432
+ job = callback_manager .call_job_fn (
433
+ cache_key ,
434
+ job_fn ,
435
+ func_args if func_args else func_kwargs ,
436
+ ctx_value ,
437
+ )
428
438
429
- ctx_value = AttributeDict ( ** context_value . get ())
430
- ctx_value . ignore_register_page = True
431
- ctx_value . pop ( "background_callback_manager" )
432
- ctx_value . pop ( "dash_response" )
439
+ data = {
440
+ "cacheKey" : cache_key ,
441
+ "job" : job ,
442
+ }
433
443
434
- job = callback_manager .call_job_fn (
435
- cache_key ,
436
- job_fn ,
437
- func_args if func_args else func_kwargs ,
438
- ctx_value ,
439
- )
444
+ cancel = long .get ("cancel" )
445
+ if cancel :
446
+ data ["cancel" ] = cancel
440
447
441
- data = {
442
- "cacheKey" : cache_key ,
443
- "job" : job ,
448
+ progress_default = long .get ("progressDefault" )
449
+ if progress_default :
450
+ data ["progressDefault" ] = {
451
+ str (o ): x for o , x in zip (progress_outputs , progress_default )
444
452
}
453
+ return to_json (data )
445
454
446
- cancel = long . get ( "cancel" )
447
- if cancel :
448
- data [ "cancel" ] = cancel
455
+ def progress_long_callback ( response , callback_manager ):
456
+ progress_outputs = long . get ( "progress" )
457
+ cache_key = flask . request . args . get ( "cacheKey" )
449
458
450
- progress_default = long .get ("progressDefault" )
451
- if progress_default :
452
- data ["progressDefault" ] = {
453
- str (o ): x for o , x in zip (progress_outputs , progress_default )
454
- }
455
- return to_json (data ), True , has_update
456
459
if progress_outputs :
457
460
# Get the progress before the result as it would be erased after the results.
458
461
progress = callback_manager .get_progress (cache_key )
459
462
if progress :
460
- response ["progress" ] = {
461
- str (x ): progress [i ] for i , x in enumerate (progress_outputs )
462
- }
463
+ response .update (
464
+ {
465
+ "progress" : {
466
+ str (x ): progress [i ] for i , x in enumerate (progress_outputs )
467
+ }
468
+ }
469
+ )
470
+
471
+ def update_long_callback (error_handler , callback_ctx , response , kwargs ):
472
+ """Set up the long callback and manage jobs."""
473
+ callback_manager = get_callback_manager (kwargs )
474
+
475
+ cache_key = flask .request .args .get ("cacheKey" )
476
+ job_id = flask .request .args .get ("job" )
463
477
464
478
output_value = callback_manager .get_result (cache_key , job_id )
479
+
480
+ progress_long_callback (response , callback_manager )
481
+
482
+ return handle_rest_long_callback (
483
+ output_value , callback_manager , response , error_handler , callback_ctx
484
+ )
485
+
486
+ async def async_update_long_callback (error_handler , callback_ctx , response , kwargs ):
487
+ """Set up the long callback and manage jobs."""
488
+ callback_manager = get_callback_manager (kwargs )
489
+
490
+ cache_key = flask .request .args .get ("cacheKey" )
491
+ job_id = flask .request .args .get ("job" )
492
+
493
+ output_value = await callback_manager .async_get_result (cache_key , job_id )
494
+
495
+ progress_long_callback (response , callback_manager )
496
+
497
+ return handle_rest_long_callback (
498
+ output_value , callback_manager , response , error_handler , callback_ctx
499
+ )
500
+
501
+ def handle_rest_long_callback (
502
+ output_value ,
503
+ callback_manager ,
504
+ response ,
505
+ error_handler ,
506
+ callback_ctx ,
507
+ has_update = False ,
508
+ ):
509
+ cache_key = flask .request .args .get ("cacheKey" )
510
+ job_id = flask .request .args .get ("job" )
465
511
# Must get job_running after get_result since get_results terminates it.
466
512
job_running = callback_manager .job_running (job_id )
467
513
if not job_running and output_value is callback_manager .UNDEFINED :
@@ -501,8 +547,8 @@ def handle_long_callback(
501
547
has_update = True
502
548
503
549
if output_value is callback_manager .UNDEFINED :
504
- return to_json (response ), True , has_update
505
- return output_value , False , has_update
550
+ return to_json (response ), has_update , True
551
+ return output_value , has_update , False
506
552
507
553
def prepare_response (
508
554
output_value ,
@@ -577,7 +623,6 @@ def prepare_response(
577
623
578
624
# pylint: disable=too-many-locals
579
625
def wrap_func (func ):
580
-
581
626
if long is not None :
582
627
long_key = BaseLongCallbackManager .register_func (
583
628
func ,
@@ -604,16 +649,18 @@ def add_context(*args, **kwargs):
604
649
605
650
try :
606
651
if long is not None :
607
- output_value , skip , has_update = handle_long_callback (
608
- kwargs ,
609
- long ,
610
- long_key ,
611
- callback_ctx ,
612
- response ,
613
- error_handler ,
614
- func ,
615
- func_args ,
616
- func_kwargs ,
652
+ if not flask .request .args .get ("cacheKey" ):
653
+ return setup_long_callback (
654
+ kwargs ,
655
+ long ,
656
+ long_key ,
657
+ func ,
658
+ func_args ,
659
+ func_kwargs ,
660
+ )
661
+
662
+ output_value , has_update , skip = update_long_callback (
663
+ error_handler , callback_ctx , response , kwargs
617
664
)
618
665
if skip :
619
666
return output_value
@@ -666,16 +713,17 @@ async def async_add_context(*args, **kwargs):
666
713
667
714
try :
668
715
if long is not None :
669
- output_value , skip , has_update = handle_long_callback (
670
- kwargs ,
671
- long ,
672
- long_key ,
673
- callback_ctx ,
674
- response ,
675
- error_handler ,
676
- func ,
677
- func_args ,
678
- func_kwargs ,
716
+ if not flask .request .args .get ("cacheKey" ):
717
+ return setup_long_callback (
718
+ kwargs ,
719
+ long ,
720
+ long_key ,
721
+ func ,
722
+ func_args ,
723
+ func_kwargs ,
724
+ )
725
+ output_value , has_update , skip = update_long_callback (
726
+ error_handler , callback_ctx , response , kwargs
679
727
)
680
728
if skip :
681
729
return output_value
@@ -695,7 +743,7 @@ async def async_add_context(*args, **kwargs):
695
743
696
744
prepare_response (
697
745
output_value ,
698
- has_output ,
746
+ output_spec ,
699
747
multi ,
700
748
response ,
701
749
callback_ctx ,
0 commit comments