a
    zn„f
;  ã                   @  sò   d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
 dd„ ZG dd„ de	ƒZG d	d
„ d
ƒZG dd„ deƒZG dd„ dƒZG dd„ dejƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )aó  
This module defines a Python-like Future and Executor for Uproot in three levels:

1. :doc:`uproot.source.futures.TrivialFuture` and
   :doc:`uproot.source.futures.TrivialExecutor`: interface only, all activity
   is synchronous.
2. :doc:`uproot.source.futures.Future`, :doc:`uproot.source.futures.Worker`,
   and :doc:`uproot.source.futures.ThreadPoolExecutor`: similar to Python's
   own Future, Thread, and ThreadPoolExecutor, though only a minimal
   implementation is provided. These exist to unify behavior between Python 2
   and 3 and provide a base class for the following.
3. :doc:`uproot.source.futures.ResourceFuture`,
   :doc:`uproot.source.futures.ResourceWorker`,
   and :doc:`uproot.source.futures.ResourceThreadPoolExecutor`: like the above
   except that a :doc:`uproot.source.chunk.Resource` is associated with every
   worker. When the threads are shut down, the resources (i.e. file handles)
   are released.

These classes implement a *subset* of Python's Future and Executor interfaces.
é    )ÚannotationsN)ÚABCÚabstractmethodc                 C  s   |  |¡‚dS )zI
    Raise an exception from a background thread on the main thread.
    N)Úwith_traceback)Zexception_classÚexception_valueÚ	traceback© r   úX/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/source/futures.pyÚdelayed_raise"   s    r
   c                   @  sB   e Zd Zdd„ Zedd„ ƒZdddœdd	„Zedd
œdd„ƒZdS )ÚExecutorc                 C  s   d| j j› dt| ƒd›d�S )Nú<z at 0xÚ012xú>)Ú	__class__Ú__name__Úid©Úselfr   r   r	   Ú__repr__*   s    zExecutor.__repr__c                O  s   t ‚dS )zv
        Submit a task to be run in the background and return a Future object
        representing that task.
        N)ÚNotImplementedError©r   ÚtaskÚargsÚkwargsr   r   r	   Úsubmit-   s    zExecutor.submitTÚbool©Úwaitc                 C  s   dS )z;
        Stop the executor and free its resources.
        Nr   )r   r   r   r   r	   Úshutdown5   s    zExecutor.shutdown©Úreturnc                 C  s   dS )zP
        True if the executor has been stopped and its resources freed.
        Fr   r   r   r   r	   Úclosed;   s    zExecutor.closedN)T)	r   Ú
__module__Ú__qualname__r   r   r   r   Úpropertyr!   r   r   r   r	   r   )   s   
r   c                   @  s0   e Zd ZdZdd„ Zddœdd„Zd
dd	„ZdS )ÚTrivialFuturezŸ
    Formally satisfies the interface for a :doc:`uproot.source.futures.Future`
    object, but it is already complete at the time when it is constructed.
    c                 C  s
   || _ d S ©N©Ú_result)r   Úresultr   r   r	   Ú__init__L   s    zTrivialFuture.__init__N)Úcontextc                C  s   || ƒ dS )z5
        The callback is called immediately.
        Nr   )r   Úcallbackr+   r   r   r	   Úadd_done_callbackO   s    zTrivialFuture.add_done_callbackc                 C  s   | j S )z5
        The result of this (Trivial)Future.
        r'   ©r   Útimeoutr   r   r	   r)   U   s    zTrivialFuture.result)N)r   r"   r#   Ú__doc__r*   r-   r)   r   r   r   r	   r%   F   s   r%   c                   @  s   e Zd ZdZdd„ ZdS )ÚTrivialExecutorzÕ
    Formally satisfies the interface for a
    :doc:`uproot.source.futures.ThreadPoolExecutor`, but the
    :ref:`uproot.source.futures.TrivialExecutor.submit` method computes its
    ``task`` synchronously.
    c                O  s   t ||i |¤ŽƒS )z3
        Immediately runs ``task(*args)``.
        )r%   r   r   r   r	   r   d   s    zTrivialExecutor.submitN)r   r"   r#   r0   r   r   r   r   r	   r1   \   s   r1   c                   @  s*   e Zd ZdZdd„ Zd	dd„Zdd„ ZdS )
ÚFutureaQ  
    Args:
        task (function): The function to evaluate.
        args (tuple): Arguments for the function.

    Like Python 3 ``concurrent.futures.Future`` except that it has only
    the subset of the interface Uproot needs and is available in Python 2.

    The :doc:`uproot.source.futures.ResourceFuture` extends this class.
    c                 C  s&   || _ || _t ¡ | _d | _d | _d S r&   )Ú_taskÚ_argsÚ	threadingÚEventÚ	_finishedr(   Ú_excinfo)r   r   r   r   r   r	   r*   z   s
    
zFuture.__init__Nc                 C  s,   | j j|d� | jdu r| jS t| jŽ  dS )zù
        Waits until the task completes (with a ``timeout``) and returns its
        result.

        If the task raises an exception in its background thread, this function
        raises that exception on the thread on which it is called.
        )r/   N)r7   r   r8   r(   r
   r.   r   r   r	   r)   �   s    
zFuture.resultc                 C  sd   z$| j d u rtdƒ‚| j | jŽ | _W n ty@   t ¡ | _Y n0 | j 	¡  | ` | `d | _ d| _d S )Nzcannot run Future twicer   )
r3   ÚRuntimeErrorr4   r(   Ú	ExceptionÚsysÚexc_infor8   r7   Úsetr   r   r   r	   Ú_run�   s    

zFuture._run)N)r   r"   r#   r0   r*   r)   r>   r   r   r   r	   r2   n   s   
r2   c                      s@   e Zd ZdZddœ‡ fdd„Zeddœdd„ƒZd	d
„ Z‡  ZS )ÚWorkerab  
    Args:
        work_queue (``queue.Queue``): The worker calls ``get`` on this queue
            for tasks in the form of :doc:`uproot.source.futures.Future`
            objects and runs them. If it ever gets a None value, the thread
            is stopped.

    A ``threading.Thread`` for the
    :doc:`uproot.source.futures.ThreadPoolExecutor`.
    úqueue.Queue©Ú
work_queuec                   s   t ƒ  ¡  d| _|| _d S ©NT)Úsuperr*   ÚdaemonÚ_work_queue)r   rB   ©r   r   r	   r*   ¨   s    
zWorker.__init__r   c                 C  s   | j S )zÑ
        The worker calls ``get`` on this queue for tasks in the form of
        :doc:`uproot.source.futures.Future` objects and runs them. If it ever
        gets a None value, the thread is stopped.
        )rF   r   r   r   r	   rB   ­   s    zWorker.work_queuec                 C  s6   d}~| j  ¡ }|du rq2t|tƒr(J ‚| ¡  qdS )z¹
        Listens to the :ref:`uproot.source.futures.Worker.work_queue` and
        executes each :doc:`uproot.source.futures.Future` it receives until it
        receives None.
        N)rF   ÚgetÚ
isinstanceÚResourceFuturer>   ©r   Úfuturer   r   r	   Úrun¶   s    
z
Worker.run)	r   r"   r#   r0   r*   r$   rB   rM   Ú__classcell__r   r   rG   r	   r?   œ   s
   r?   c                   @  sv   e Zd ZdZdddœdd„Zdd„ Zed	d
œdd„ƒZed	d
œdd„ƒZedd
œdd„ƒZ	dd„ Z
dddœdd„ZdS )ÚThreadPoolExecutoraÉ  
    Args:
        max_workers (None or int): The maximum number of workers to start.
        In the current implementation this is exactly the number of workers.
        If None, use ``os.cpu_count()``.

    Like Python 3 ``concurrent.futures.ThreadPoolExecutor`` except that it has
    only the subset of the interface Uproot needs and is available in Python 2.

    The :doc:`uproot.source.futures.ResourceThreadPoolExecutor` extends this
    class.
    Nz
int | None)Úmax_workersc                 C  sz   |d u r0t tdƒrt ¡ | _ndd l}| ¡ | _t ¡ | _g | _t	| jƒD ]}| j 
t| jƒ¡ qJ| jD ]}| ¡  qhd S )NÚ	cpu_countr   )ÚhasattrÚosrQ   Ú_max_workersÚmultiprocessingÚqueueÚQueuerF   Ú_workersÚrangeÚappendr?   Ústart)r   rP   rU   Ú_Úworkerr   r   r	   r*   Ô   s    



zThreadPoolExecutor.__init__c                 C  s(   d| j j› dt| jƒ› dt| ƒd›d�S )Nr   z (z workers) at 0xr   r   )r   r   ÚlenrX   r   r   r   r   r	   r   ä   s    zThreadPoolExecutor.__repr__Úintr   c                 C  s   | j S )z0
        The maximum number of workers.
        )rT   r   r   r   r	   rP   ç   s    zThreadPoolExecutor.max_workersc                 C  s
   t | jƒS )z(
        The number of workers.
        )r^   rX   r   r   r   r	   Únum_workersî   s    zThreadPoolExecutor.num_workerszlist[Worker]c                 C  s   | j S )zJ
        A list of workers (:doc:`uproot.source.futures.Worker`).
        )rX   r   r   r   r	   Úworkersõ   s    zThreadPoolExecutor.workersc                O  s   t ||ƒ}| j |¡ |S )zå
        Pass the ``task`` and ``args`` onto the workers'
        :ref:`uproot.source.futures.Worker.work_queue` as a
        :doc:`uproot.source.futures.Future` so that it will be executed when
        one is available.
        )r2   rF   Úput)r   r   r   r   rL   r   r   r	   r   ü   s    
zThreadPoolExecutor.submitTr   r   c                 C  sH   | j D ]}| ¡ r| j d¡ qtdd„ | j D ƒƒrDt d¡ q qDq dS )zÈ
        Stop every :doc:`uproot.source.futures.Worker` by putting None
        on the :ref:`uproot.source.futures.Worker.work_queue` until none of
        them satisfy ``worker.is_alive()``.
        Nc                 s  s   | ]}|  ¡ V  qd S r&   )Úis_alive)Ú.0r]   r   r   r	   Ú	<genexpr>  ó    z.ThreadPoolExecutor.shutdown.<locals>.<genexpr>gü©ñÒMbP?)rX   rc   rF   rb   ÚanyÚtimeÚsleep)r   r   r]   r   r   r	   r     s    
zThreadPoolExecutor.shutdown)N)T)r   r"   r#   r0   r*   r   r$   rP   r`   ra   r   r   r   r   r   r	   rO   Æ   s   rO   c                      s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
rJ   aB  
    Args:
        task (function): The function to evaluate with a
            :doc:`uproot.source.chunk.Resource` as its first argument.

    A :doc:`uproot.source.futures.Future` that uses the
    :doc:`uproot.source.chunk.Resource` associated with the
    :doc:`uproot.source.futures.ResourceWorker` that runs it.
    c                   s   t ƒ  |d ¡ d | _d S r&   )rD   r*   Ú_notify)r   r   rG   r   r	   r*   %  s    zResourceFuture.__init__c                 C  s
   || _ dS )zX
        Set the ``notify`` function that is called when this task is complete.
        N)rj   )r   Únotifyr   r   r	   Ú_set_notify)  s    zResourceFuture._set_notifyc                 C  s0   | j  ¡ s,|| _| j  ¡  | jd ur,|  ¡  d S r&   )r7   Úis_setr8   r=   rj   )r   Úexcinfor   r   r	   Ú_set_excinfo/  s
    


zResourceFuture._set_excinfoc                 C  sX   z|   |¡| _W n ty,   t ¡ | _Y n0 | j ¡  | jd urT|  ¡  | `d | _d S r&   )	r3   r(   r:   r;   r<   r8   r7   r=   rj   ©r   Úresourcer   r   r	   r>   6  s    

zResourceFuture._run)	r   r"   r#   r0   r*   rl   ro   r>   rN   r   r   rG   r	   rJ     s
   
rJ   c                      s:   e Zd ZdZddœ‡ fdd„Zedd„ ƒZdd	„ Z‡  ZS )
ÚResourceWorkera  
    Args:
        work_queue (``queue.Queue``): The worker calls ``get`` on this queue
            for tasks in the form of :doc:`uproot.source.futures.Future`
            objects and runs them. If it ever gets a None value, the thread
            is stopped.

    A :doc:`uproot.source.futures.Worker` that is bound to a
    :doc:`uproot.source.chunk.Resource`. This
    :ref:`uproot.source.futures.ResourceWorker.resource` is the first argument
    passed to each :doc:`uproot.source.futures.ResourceFuture` that it
    executes.
    r@   rA   c                   s   t ƒ  |¡ || _d S r&   )rD   r*   Ú	_resource)r   rB   rq   rG   r   r	   r*   Q  s    zResourceWorker.__init__c                 C  s   | j S )zW
        The :doc:`uproot.source.chunk.Resource` that is bound to this worker.
        )rs   r   r   r   r	   rq   U  s    zResourceWorker.resourcec                 C  s:   d}~| j  ¡ }|du rq6t|tƒs(J ‚| | j¡ qdS )a#  
        Listens to the :ref:`uproot.source.futures.ResourceWorker.work_queue`
        and executes each :doc:`uproot.source.futures.ResourceFuture` it
        receives (with :ref:`uproot.source.futures.ResourceWorker.resource` as
        its first argument) until it receives None.
        N)rF   rH   rI   rJ   r>   rs   rK   r   r   r	   rM   \  s    
zResourceWorker.run)	r   r"   r#   r0   r*   r$   rq   rM   rN   r   r   rG   r	   rr   B  s
   
rr   c                   @  sR   e Zd ZdZdd„ Zdddœdd„Zdd	„ Zed
dœdd„ƒZdd„ Z	dd„ Z
dS )ÚResourceThreadPoolExecutora  
    Args:
        resources (list of :doc:`uproot.source.chunk.Resource`): Resources to
            wrap as :doc:`uproot.source.futures.ResourceFuture` objects.

    A :doc:`uproot.source.futures.ThreadPoolExecutor` whose workers are bound
    to resources, such as file handles.
    c                 C  s`   d| _ t|ƒdk rtdƒ‚t ¡ | _g | _|D ]}| j t| j|ƒ¡ q.| jD ]}| 	¡  qNd S )NFé   zat least one worker is required)
Ú_closedr^   Ú
ValueErrorrV   rW   rF   rX   rZ   rr   r[   )r   Ú	resourcesrq   r]   r   r   r	   r*   w  s    

z#ResourceThreadPoolExecutor.__init__rJ   ©rL   r    c                 C  s<   t |tƒsJ ‚| jr,td| jd jj› �ƒ‚| j |¡ |S )a6  
        Pass the ``task`` onto the workers'
        :ref:`uproot.source.futures.ResourceWorker.work_queue` as a
        :doc:`uproot.source.futures.ResourceFuture` so that it will be
        executed with its :ref:`uproot.source.futures.ResourceWorker.resource`
        when that worker is available.
        úresource is closed for file r   )	rI   rJ   r!   ÚOSErrorrX   rq   Ú	file_pathrF   rb   rK   r   r   r	   r   „  s    ÿz!ResourceThreadPoolExecutor.submitc                 C  s   |   ddd¡ dS )z�
        Stops all :doc:`uproot.source.futures.ResourceWorker` threads and frees
        their :ref:`uproot.source.futures.ResourceWorker.resource`.
        N©Ú__exit__r   r   r   r	   Úclose”  s    z ResourceThreadPoolExecutor.closer   r   c                 C  s   | j S )z¹
        True if the :doc:`uproot.source.futures.ResourceWorker` threads have
        been stopped and their
        :ref:`uproot.source.futures.ResourceWorker.resource` freed.
        ©rv   r   r   r   r	   r!   ›  s    z!ResourceThreadPoolExecutor.closedc                 C  s   | j D ]}|j ¡  qd S r&   )rX   rq   Ú	__enter__)r   r]   r   r   r	   r�   ¤  s    
z$ResourceThreadPoolExecutor.__enter__c                 C  s.   |   ¡  | jD ]}|j |||¡ qd| _d S rC   )r   rX   rq   r~   rv   )r   Úexception_typer   r   r]   r   r   r	   r~   ¨  s    
z#ResourceThreadPoolExecutor.__exit__N©r   r"   r#   r0   r*   r   r   r$   r!   r�   r~   r   r   r   r	   rt   m  s   	rt   c                   @  sR   e Zd ZdZdd„ Zdddœdd„Zdd	„ Zed
dœdd„ƒZdd„ Z	dd„ Z
dS )ÚResourceTrivialExecutora
  
    Args:
        resource (:doc:`uproot.source.chunk.Resource`): Resource to
            wrap as :doc:`uproot.source.futures.ResourceFuture` object.

    A :doc:`uproot.source.futures.ResourceTrivialExecutor` is bound
    to a resource, such as a file handle.
    c                 C  s   || _ d| _d S )NF)rs   rv   rp   r   r   r	   r*   ¼  s    z ResourceTrivialExecutor.__init__rJ   ry   c                 C  s6   t |tƒsJ ‚| jr&td| jj› �ƒ‚| | j¡ |S )z 
        Pass the ``task`` as a
        :doc:`uproot.source.futures.ResourceFuture` so that it will be
        executed with its :ref:`self._resource`.
        rz   )rI   rJ   r!   r{   rs   r|   r>   rK   r   r   r	   r   À  s
    zResourceTrivialExecutor.submitc                 C  s   |   ddd¡ dS )zP
        Stops the :doc:`uproot.source.futures.ResourceTrivialExecutor`
        Nr}   r   r   r   r	   r   Ì  s    zResourceTrivialExecutor.closer   r   c                 C  s   | j S )zl
        True if the :doc:`uproot.source.futures.ResourceTrivialExecutor` has
        been stopped.
        r€   r   r   r   r	   r!   Ò  s    zResourceTrivialExecutor.closedc                 C  s   | j  ¡  d S r&   )rs   r�   r   r   r   r	   r�   Ú  s    z!ResourceTrivialExecutor.__enter__c                 C  s"   |   ¡  | j |||¡ d| _d S rC   )r   rs   r~   rv   )r   r‚   r   r   r   r   r	   r~   Ý  s    z ResourceTrivialExecutor.__exit__Nrƒ   r   r   r   r	   r„   ²  s   	r„   )r0   Ú
__future__r   rS   rV   r;   r5   rh   Úabcr   r   r
   r   r%   r1   r2   ÚThreadr?   rO   rJ   rr   rt   r„   r   r   r   r	   Ú<module>   s$   .*T(+E