o
    d                     @   st   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 deZdd ZG dd deZdS )    N)copy_context   )BaseLongCallbackManager   )context_value)AttributeDict)PreventUpdatez__$pending__c                       sl   e Zd ZdZd fdd	Zdd Zdd Zd	d
 ZdddZdd Z	dd Z
dd Zdd Zdd Z  ZS )DiskcacheManagerz^Manage the background execution of callbacks with subprocesses and a diskcache result backend.Nc              
      s   zddl }ddl}ddl}W n ty } ztd|d}~ww |du r*| | _nt||j|jfs7t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__ r/var/www/html/visualizacion-main/env/lib/python3.10/site-packages/dash/long_callback/managers/diskcache_manager.pyr      s*   zDiskcacheManager.__init__c              
   C   s   dd l }|d u r
d S t|}| j b ||r]||}|jddD ]}z|  W q$ |jy6   Y q$w z|  W n
 |jyG   Y nw z|	d W n |j
|jfy\   Y nw W d    d S W d    d S W d    d S 1 sxw   Y  d S )Nr   T)	recursiver   )r   intr   transact
pid_existsProcesschildrenkillNoSuchProcesswaitTimeoutExpired)r   jobr   processprocr   r   r   terminate_job:   s:   

"zDiskcacheManager.terminate_jobc                 C   s:   dd l }t|}|r||r| |s| | dS dS )Nr   TF)r   r   r!   job_runningr+   )r   r(   r   r   r   r   terminate_unhealthy_jobX   s   

z(DiskcacheManager.terminate_unhealthy_jobc                 C   s:   dd l }t|}|r||r||}| |jkS dS )Nr   F)r   r   r!   r"   statusSTATUS_ZOMBIE)r   r(   r   r*   r   r   r   r,   d   s   
zDiskcacheManager.job_runningc                 C   s   t || j|S N)_make_job_fnr   )r   fnprogresskeyr   r   r   make_job_fnn   s   zDiskcacheManager.make_job_fnc                 C   s   | j | d S r0   )r   deleter   r4   r   r   r   clear_cache_entryq      z"DiskcacheManager.clear_cache_entryc                 C   s4   ddl m} |||| |||fd}|  |jS )Nr   )r"   )targetargs)r   r"   _make_progress_keystartpid)r   r4   job_fnr;   contextr"   r*   r   r   r   call_job_fnu   s   zDiskcacheManager.call_job_fnc                 C   s*   |  |}| j|}|r| j| |S r0   )r<   r   getr6   )r   r4   progress_keyprogress_datar   r   r   get_progress   s
   
zDiskcacheManager.get_progressc                 C   s   | j |d uS r0   )r   rB   r7   r   r   r   result_ready   r9   zDiskcacheManager.result_readyc                 C   sp   | j || j}|| ju r| jS | jd u r| | n| jr'| j j|| jd | | | |r6| | |S )N)r   )	r   rB   	UNDEFINEDr   r8   r   touchr<   r+   )r   r4   r(   resultr   r   r   
get_result   s   


zDiskcacheManager.get_result)NNNr0   )__name__
__module____qualname____doc__r   r+   r-   r,   r5   r8   rA   rE   rF   rJ   __classcell__r   r   r   r   r	      s    +

r	   c                    s    fdd}|S )Nc                    sF   fdd}r|gng t  } fdd}|| d S )Nc                    s$   t | ttfs
| g}  |  d S r0   )r   listtupleset)progress_value)r   rC   r   r   _set_progress   s   z3_make_job_fn.<locals>.job_fn.<locals>._set_progressc               
      s   t di } d| _t|  z)ttri }ntttfr.g R  }n	g R  }W n3 tyJ    ddi Y d S  t	yk } z dt
|t di W Y d }~d S d }~ww  | d S )NF_dash_no_updatelong_callback_error)msgtbr   )r   ignore_register_pager   rR   r   dictrP   rQ   r   	Exceptionstr	traceback
format_exc)cuser_callback_outputerr)r   r@   r2   maybe_progress
result_keyuser_callback_argsr   r   run   s0   

z)_make_job_fn.<locals>.job_fn.<locals>.run)r   re   )rc   rC   rd   r@   rT   ctxre   r   r2   r3   )r@   rb   rC   rc   rd   r   r?      s
   z_make_job_fn.<locals>.job_fnr   )r2   r   r3   r?   r   rg   r   r1      s   'r1   c                   @   s   e Zd ZdZdS )DiskcacheLongCallbackManagerz<Deprecated: use `from dash import DiskcacheManager` instead.N)rK   rL   rM   rN   r   r   r   r   rh      s    rh   )r]   contextvarsr    r   _callback_contextr   _utilsr   
exceptionsr   _pending_valuer	   r1   rh   r   r   r   r   <module>   s     +