U
    /e                     @   s\   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 )
    N)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 n8 tk
rD } ztdjt|d|W 5 d }~X Y nX |j| _t|trl|j|f||}n|st|rxt	|| _
t | _d S )Nr   z/Cache requires cachey, "{ex}" problem importing)ex)cacheyImportErrorformatstrnbytes_nbytes
isinstancer   r   AssertionErrorcachedict
starttimes)selfr   argskwargsr	   r   r   r   ./tmp/pip-unpacked-wheel-dbjnr7gq/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   _start.   s    zCache._startc                 C   s   t  | j|< d S r   )r   r   )r   r   r   stater   r   r   _pretask4   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>;   s     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   	_posttask7   s    
zCache._posttaskc                 C   s   | j   | j  d S r   )r   clearr   )r   r   r!   Zerroredr   r   r   _finish@   s    
zCache._finishN)	__name__
__module____qualname____doc__r   r    r"   r2   r4   r   r   r   r   r   
   s   	r   )
r*   Znumbersr   Ztimeitr   Zdask.callbacksr   r+   r)   r   r   r   r   r   <module>   s
   