
    $g6                         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y)    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.c                    	 ddl }ddl}ddl}||j	                         | _        n4t        ||j                  |j                  f      st        d      || _        || _	        t        | -  |       y# t        $ r}t        d      |d}~ww xY w)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	           j/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/dash/long_callback/managers/diskcache_manager.pyr   zDiskcacheManager.__init__   s    $
	# =#//+DKeiooy7L7L%MN 6   DK"'  	#)
 ##	#s   A2 2	B;BBc                    dd l }|y t        |      }| j                  j                         5  |j	                  |      r\|j                  |      }|j                  d      D ]  }	 |j                           	 |j                          	 |j                  d       d d d        y # |j                  $ r Y Sw xY w# |j                  $ r Y Aw xY w# |j                  |j                  f$ r Y Pw xY w# 1 sw Y   y xY w)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    ;#h [[!!# 	  % ..-#,,t,< D		LLNLLO!	 	 "// 
 ++ 
 --v/C/CD #	 	sk   8C/&B$6C/9B9
C$B63C/5B66C/9CC/
CC/C,)C/+C,,C//C8c                     dd l }t        |      }|r4|j                  |      r#| j                  |      s| j	                  |       yy)Nr   TF)r   r#   r%   job_runningr/   )r   r,   r   s      r    terminate_unhealthy_jobz(DiskcacheManager.terminate_unhealthy_jobY   s@    #h6$$S)##C(""3'    c                     dd l }t        |      }|r?|j                  |      r.|j                  |      }|j	                         |j
                  k7  S y)Nr   F)r   r#   r%   r&   statusSTATUS_ZOMBIE)r   r,   r   r.   s       r    r1   zDiskcacheManager.job_runninge   sF    #h6$$S)>>#&D;;=F$8$888r3   c                 0    t        || j                  |      S N)_make_job_fnr   )r   fnprogresskeys       r    make_job_fnzDiskcacheManager.make_job_fno   s    BX66r3   c                 :    | j                   j                  |       y r8   )r   deleter   r<   s     r    clear_cache_entryz"DiskcacheManager.clear_cache_entryr   s    3r3   c                     ddl m}  |||| j                  |      ||f      }|j                          |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?    ( t..s3T7C
 	

xxr3   c                     | j                  |      }| j                  j                  |      }|r| j                  j                  |       |S r8   )rE   r   getr?   )r   r<   progress_keyprogress_datas       r    get_progresszDiskcacheManager.get_progress   s?    ..s35KK|,r3   c                 <    | j                   j                  |      d uS r8   )r   rL   r@   s     r    result_readyzDiskcacheManager.result_ready   s    {{s#4//r3   c                    | j                   j                  || j                        }|| j                  u r| j                  S | j                  | j	                  |       n3| j
                  r'| j                   j                  || j
                         | j	                  | j                  |             |r| j                  |       |S )N)r   )	r   rL   	UNDEFINEDr   rA   r   touchrE   r/   )r   r<   r,   results       r    
get_resultzDiskcacheManager.get_result   s    dnn5T^^#>>! == ""3'{{!!#dkk!:t66s;<s#r3   c                     | j                  |      }| j                  j                  || j                        }|| j                  u ri S | j	                  |       |S r8   )_make_set_props_keyr   rL   rS   rA   )r   r<   set_props_keyrU   s       r    get_updated_propsz"DiskcacheManager.get_updated_props   sN    00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)#V<
7 
0&r3   r   c                       fd}|S )Nc                      	fd}r|gng 	 fdt               }	
 fd}|j                  |       y )Nc                 \    t        | t        t        f      s| g} j                  |        y r8   )r   listtupleset)progress_valuer   rM   s    r    _set_progressz3_make_job_fn.<locals>.job_fn.<locals>._set_progress   s'    ntUm<"0!1IIlN3r3   c                 4    j                   d| |i       y )Nz
-set_props)re   )_idpropsr   
result_keys     r    
_set_propsz0_make_job_fn.<locals>.job_fn.<locals>._set_props   s    IIJ/#u>r3   c                      t        di } d| _        t              | _        t	        j
                  |        d}	 t        
t              r	 i 
}n+t        
t        t        f      r g 
 }n
 g 
 }|sj                  	       y y # t        $ r d}j                  	ddi       Y 7t        $ r>}d}j                  	dt        |      t        j                         di       Y d }~xd }~ww xY w)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7AOa G0$7+-~+TAS+T( 2T5MB+-+S~+S@R+S(+-+R~+R?Q+R(  		*&:;  ! N		*'8:K&LM 
		-#&s8"+"6"6"80 
s   AB C=7C=?4C88C=)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 -5-"	? n	< 	<> 	r3   rr   )r:   r   r;   rH   s   ``` r    r9   r9      s    -^ Mr3   c                       e Zd ZdZy)DiskcacheLongCallbackManagerz<Deprecated: use `from dash import DiskcacheManager` instead.N)r[   r\   r]   r^   rr   r3   r    r   r      s    Fr3   r   )rx   contextvarsr    r   _proxy_set_propsr   _callback_contextr	   _utilsr
   
exceptionsr   _pending_valuer   r9   r   rr   r3   r    <module>r      sD     $ % , . # '[. [|0fG#3 Gr3   