
    Mf                         d dl Z d dlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 d	Z G d
 de          Zd Z G d de          ZdS )    N)copy_context   )BaseLongCallbackManager   )context_value)AttributeDict)PreventUpdatez__$pending__c                   \     e Zd ZdZd fd	Zd Zd Zd ZddZd Z	d	 Z
d
 Zd Zd Z xZS )DiskcacheManagerz^Manage the background execution of callbacks with subprocesses and a diskcache result backend.Nc                 T   	 ddl }ddl}ddl}n"# t          $ r}t          d          |d}~ww xY w||                                | _        n2t          ||j        |j        f          st          d          || _        || _	        t                                          |           dS )a  
        Long callback manager that runs callback logic in a subprocess and stores
        results on disk using diskcache

        :param cache:
            A diskcache.Cache or diskcache.FanoutCache instance. See the diskcache
            documentation for information on configuration options. If not provided,
            a diskcache.Cache instance will be created with default values.
        :param cache_by:
            A list of zero-argument functions.  When provided, caching is enabled and
            the return values of these functions are combined with the callback
            function's input arguments and source code to generate cache keys.
        :param expire:
            If provided, a cache entry will be removed when it has not been accessed
            for ``expire`` seconds.  If not provided, the lifetime of cache entries
            is determined by the default behavior of the ``cache`` instance.
        r   Nz{DiskcacheLongCallbackManager requires extra dependencies which can be installed doing

    $ pip install "dash[diskcache]"
zHFirst argument must be a diskcache.Cache or diskcache.FanoutCache object)	diskcachepsutilmultiprocessImportErrorCachehandle
isinstanceFanoutCache
ValueErrorexpiresuper__init__)	selfcachecache_byr   r   r   r   missing_imports	__class__s	           l/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/dash/long_callback/managers/diskcache_manager.pyr   zDiskcacheManager.__init__   s    $
	#MMM 	# 	# 	#) 
 ##	# =#//++DKKeioy7L%MNN  6    DK"""""s    
/*/c                    dd l }|d S t          |          }| j                                        5  |                    |          r|                    |          }|                    d          D ]'}	 |                                 # |j        $ r Y $w xY w	 |                                 n# |j        $ r Y nw xY w	 |	                    d           n# |j
        |j        f$ r Y nw xY wd d d            d S # 1 swxY w Y   d S )Nr   T)	recursiver   )r   intr   transact
pid_existsProcesschildrenkillNoSuchProcesswaitTimeoutExpired)r   jobr   processprocs        r   terminate_jobzDiskcacheManager.terminate_job:   s   ;F#hh [!!## 	 	  %%  ..--#,,t,<<  D		!/   LLNNNN+   DLLOOOO-v/CD   D#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s~   AC;5B
	C;

BC;BC;B0/C;0
B=:C;<B==C;CC;C+(C;*C++C;;C?C?c                     dd l }t          |          }|rA|                    |          r,|                     |          s|                     |           dS dS )Nr   TF)r   r!   r#   job_runningr-   )r   r*   r   s      r   terminate_unhealthy_jobz(DiskcacheManager.terminate_unhealthy_jobX   sg    #hh 	6$$S)) 	##C(( ""3'''tu    c                     dd l }t          |          }|rG|                    |          r2|                    |          }|                                |j        k    S dS )Nr   F)r   r!   r#   r$   statusSTATUS_ZOMBIE)r   r*   r   r,   s       r   r/   zDiskcacheManager.job_runningd   s_    #hh 	96$$S)) 	9>>#&&D;;==F$888ur1   c                 .    t          || j        |          S N)_make_job_fnr   )r   fnprogresskeys       r   make_job_fnzDiskcacheManager.make_job_fnn   s    BX666r1   c                 :    | j                             |           d S r6   )r   deleter   r:   s     r   clear_cache_entryz"DiskcacheManager.clear_cache_entryq   s    3r1   c                     ddl m}  ||||                     |          ||f          }|                                 |j        S )Nr   )r$   )targetargs)r   r$   _make_progress_keystartpid)r   r:   job_fnrB   contextr$   r,   s          r   call_job_fnzDiskcacheManager.call_job_fnu   s^    (((((( wd&=&=c&B&BD' R
 
 
 	

xr1   c                     |                      |          }| j                            |          }|r| j                            |           |S r6   )rC   r   getr=   )r   r:   progress_keyprogress_datas       r   get_progresszDiskcacheManager.get_progress   sL    ..s3355 	-K|,,,r1   c                 :    | j                             |          d uS r6   )r   rJ   r>   s     r   result_readyzDiskcacheManager.result_ready   s    {s##4//r1   c                 n   | j                             || j                  }|| j        u r| j        S | j        |                     |           n(| j        r!| j                             || j                   |                     |                     |                     |r|                     |           |S )N)r   )	r   rJ   	UNDEFINEDr   r?   r   touchrC   r-   )r   r:   r*   results       r   
get_resultzDiskcacheManager.get_result   s    dn55T^##>! = ""3''''{ ;!!#dk!:::t66s;;<<< 	$s###r1   )NNNr6   )__name__
__module____qualname____doc__r   r-   r0   r/   r;   r?   rH   rM   rO   rT   __classcell__)r   s   @r   r   r      s        hh)# )# )# )# )# )#V  <
 
 
  7 7 7 7     	 	 	  0 0 0      r1   r   c                       fd}|S )Nc                      fd}
r|gng t                      }	 fd}|                    |           d S )Nc                 r    t          | t          t          f          s| g}                     |            d S r6   )r   listtupleset)progress_valuer   rK   s    r   _set_progressz3_make_job_fn.<locals>.job_fn.<locals>._set_progress   s;    ntUm<< 2"0!1IIlN33333r1   c                     t          di } d| _        t          j        |            	 t	          t
                    r	 i }n1t	          t          t          f          r g R  }n
 g R  }                    |           d S # t          $ r                     ddi           Y d S t          $ rD}                    dt          |          t          j                    di           Y d }~d S d }~ww xY w)NF_dash_no_updatelong_callback_error)msgtb )r   ignore_register_pager   r_   r   dictr]   r^   r	   	Exceptionstr	traceback
format_exc)	cuser_callback_outputerrr   rG   r8   maybe_progress
result_keyuser_callback_argss	      r   runz)_make_job_fn.<locals>.job_fn.<locals>.run   s   ((((A%*A"a   <0$77 S+-2~+TAS+T+T(( 2T5MBB S+-2+S~+S@R+S+S+S((+-2+R~+R?Q+R+R+R( 		*&:;;;;; ! N N N		*'8:K&LMMMMMM 	 	 			-#&s88"+"6"8"80 0        	s   AB "D6	D?9C>>D)r   rt   )rr   rK   rs   rG   ra   ctxrt   rq   r   r8   r9   s   ````   @r   rF   z_make_job_fn.<locals>.job_fn   s    	4 	4 	4 	4 	4 	4 -5<-"nn	< 	< 	< 	< 	< 	< 	< 	< 	< 	<4 	r1   rg   )r8   r   r9   rF   s   ``` r   r7   r7      s0    % % % % % % %N Mr1   c                       e Zd ZdZdS )DiskcacheLongCallbackManagerz<Deprecated: use `from dash import DiskcacheManager` instead.N)rU   rV   rW   rX   rg   r1   r   rw   rw      s        FFFFr1   rw   )rl   contextvarsr    r   _callback_contextr   _utilsr   
exceptionsr	   _pending_valuer   r7   rw   rg   r1   r   <module>r~      s        $ $ $ $ $ $ % % % % % % . . . . . . # # # # # # ' ' ' ' ' 'P P P P P. P P Pf( ( (VG G G G G#3 G G G G Gr1   