a
    tDf                     @  sh   d dl mZ d dlZd dlmZ d dlmZ d dlmZ e	dd e	dd  Z
G d	d
 d
eZdS )    )annotationsN)Number)default_timer)CallbackgGz?    c                   @  s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )Cachea  Use cache for computation

    Examples
    --------

    >>> cache = Cache(1e9)  # doctest: +SKIP

    The cache can be used locally as a context manager around ``compute`` or
    ``get`` calls:

    >>> with cache:  # doctest: +SKIP
    ...     result = x.compute()

    You can also register a cache globally, so that it works for all
    computations:

    >>> cache.register()    # doctest: +SKIP
    >>> cache.unregister()  # doctest: +SKIP
    c              
   O  s   zdd l }W n: tyF } z"tdjt|d|W Y d }~n
d }~0 0 |j| _t|trt|j|g|R i |}n|s||rJ || _	t
 | _d S )Nr   z/Cache requires cachey, "{ex}" problem importing)ex)cacheyImportErrorformatstrnbytes_nbytes
isinstancer   r   cachedict
starttimes)selfr   argskwargsr
   r	   r   r   W/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/dask/cache.py__init__!   s    
zCache.__init__c                 C  s:   t  | _t|t| jj@ }|D ]}| jj| ||< q d S N)r   	durationssetr   data)r   dskoverlapkeyr   r   r   _start0   s    zCache._startc                 C  s   t  | j|< d S r   )r   r   )r   r   r   stater   r   r   _pretask6   s    zCache._pretaskc           	        s   t   j|  }|d | }|r:|t fdd|D 7 }| j|<  |t t|d  } jj	|||| d |d d S )NZdependenciesc                 3  s   | ]} j |d V  qdS )r   N)r   get).0kr   r   r   	<genexpr>=       z"Cache._posttask.<locals>.<genexpr>r   g    eA)Zcostr   )
r   r   maxr   r   overheadsys	getsizeofr   put)	r   r   valuer   r!   iddurationdepsnbr   r&   r   	_posttask9   s    
zCache._posttaskc                 C  s   | j   | j  d S r   )r   clearr   )r   r   r!   Zerroredr   r   r   _finishB   s    
zCache._finishN)	__name__
__module____qualname____doc__r   r    r"   r3   r5   r   r   r   r   r      s   	r   )
__future__r   r+   Znumbersr   Ztimeitr   Zdask.callbacksr   r,   r*   r   r   r   r   r   <module>   s   