
     "j6                         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d
lmZ dZ G d de          Zd Z G d de          ZdS )    N)copy_context   )BaseLongCallbackManager   )ProxySetProps   )context_value)AttributeDict)PreventUpdatez__$pending__c                   b     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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	           i/var/www/html/bolsaweb/venv/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_jobY   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    r1   zDiskcacheManager.job_runninge   s_    #hh 	96$$S)) 	9>>#&&D;;==F$888ur3   c                 .    t          || j        |          S N)_make_job_fnr   )r   fnprogresskeys       r    make_job_fnzDiskcacheManager.make_job_fno   s    BX666r3   c                 :    | j                             |           d S r8   )r   deleter   r<   s     r    clear_cache_entryz"DiskcacheManager.clear_cache_entryr   s    3r3   c                     ddl m}  ||||                     |          ||f          }|                                 |j        S )Nr   )r&   )targetargs)r   r&   _make_progress_keystartpid)r   r<   job_fnrD   contextr&   r.   s          r    call_job_fnzDiskcacheManager.call_job_fnv   s_    (((((( wt..s33T7C
 
 
 	

xr3   c                     |                      |          }| j                            |          }|r| j                            |           |S r8   )rE   r   getr?   )r   r<   progress_keyprogress_datas       r    get_progresszDiskcacheManager.get_progress   sL    ..s3355 	-K|,,,r3   c                 :    | j                             |          d uS r8   )r   rL   r@   s     r    result_readyzDiskcacheManager.result_ready   s    {s##4//r3   c                 n   | j                             || j                  }|| j        u r| j        S | j        |                     |           n(| j        r!| j                             || j                   |                     |                     |                     |r|                     |           |S )N)r   )	r   rL   	UNDEFINEDr   rA   r   touchrE   r/   )r   r<   r,   results       r    
get_resultzDiskcacheManager.get_result   s    dn55T^##>! = ""3''''{ ;!!#dk!:::t66s;;<<< 	$s###r3   c                     |                      |          }| j                            || j                  }|| j        u ri S |                     |           |S r8   )_make_set_props_keyr   rL   rS   rA   )r   r<   set_props_keyrU   s       r    get_updated_propsz"DiskcacheManager.get_updated_props   sW    0055??T^##I}---r3   )NNNr8   )__name__
__module____qualname____doc__r   r/   r2   r1   r=   rA   rJ   rO   rQ   rV   rZ   __classcell__)r   s   @r    r   r      s        hh)# )# )# )# )# )#V  <
 
 
  7 7 7 7     
 
 
  0 0 0  &      r3   r   c                       fd}|S )Nc                      	fd}r|gng 	 fdt                      }	
 fd}|                    |           d S )Nc                 r    t          | t          t          f          s| g}                     |            d S r8   )r   listtupleset)progress_valuer   rM   s    r    _set_progressz3_make_job_fn.<locals>.job_fn.<locals>._set_progress   s;    ntUm<< 2"0!1IIlN33333r3   c                 >                          d| |i           d S )Nz
-set_props)re   )_idpropsr   
result_keys     r    
_set_propsz0_make_job_fn.<locals>.job_fn.<locals>._set_props   s*    II///#u>>>>>r3   c                  F   t          di } d| _        t                    | _        t	          j        |            d}	 t          
t                    r	 i 
}n1t          
t          t          f          r g 
R  }n
 g 
R  }nw# t          $ r d}                    	ddi           Y nQt          $ rE}d}                    	dt          |          t          j                    di           Y d }~nd }~ww xY w|s                    	|           d S d S )NFT_dash_no_updatelong_callback_error)msgtb )r
   ignore_register_pager   updated_propsr	   re   r   dictrc   rd   r   	Exceptionstr	traceback
format_exc)cerroreduser_callback_outputerrrl   r   rI   r:   maybe_progressrk   user_callback_argss       r    runz)_make_job_fn.<locals>.job_fn.<locals>.run   s   ((((A%*A"+J77AOa   G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 
 
 
		-#&s88"+"6"8"80 0       
  <		*&:;;;;;< <s    AB $D6	D?;C??D)r   r   )rk   rM   r   rI   rg   ctxr   rl   r~   r   r:   r;   s   ````   @@r    rH   z_make_job_fn.<locals>.job_fn   s    	4 	4 	4 	4 	4 	4 -5<-"	? 	? 	? 	? 	? 	? nn	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<> 	r3   rr   )r:   r   r;   rH   s   ``` r    r9   r9      s0    - - - - - - -^ Mr3   c                       e Zd ZdZdS )DiskcacheLongCallbackManagerz<Deprecated: use `from dash import DiskcacheManager` instead.N)r[   r\   r]   r^   rr   r3   r    r   r      s        FFFFr3   r   )rx   contextvarsr    r   _proxy_set_propsr   _callback_contextr	   _utilsr
   
exceptionsr   _pending_valuer   r9   r   rr   r3   r    <module>r      s       $ $ $ $ $ $ % % % % % % , , , , , , . . . . . . # # # # # # ' ' ' ' ' '[ [ [ [ [. [ [ [|0 0 0fG G G G G#3 G G G G Gr3   