o
    %h	                     @   sj   d dl mZmZmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ eZdgZG dd deZdS )    )ExecutorFutureThreadPoolExecutor)TracebackType)AnyCallableDictOptionalTupleType)EventEmitterExecutorEventEmitterc                       s   e Zd ZdZddedee ddf fddZdeded	e	e
d
f deee
f ddf
ddZddededdfddZdedd fddZdedee dededee f
ddZ  ZS )r   a  An event emitter class which runs handlers in a `concurrent.futures`
    executor.

    By default, this class creates a default `ThreadPoolExecutor`, but
    a custom executor may also be passed in explicitly to, for instance,
    use a `ProcessPoolExecutor` instead.

    This class runs all emitted events on the configured executor. Errors
    captured by the resulting Future are automatically emitted on the
    `error` event. This is unlike the EventEmitter, which have no error
    handling.

    The underlying executor may be shut down by calling the `shutdown`
    method. Alternately you can treat the event emitter as a context manager:

    ```py
    with ExecutorEventEmitter() as ee:
        # Underlying executor open

        @ee.on('data')
        def handler(data):
            print(data)

        ee.emit('event')

    # Underlying executor closed
    ```

    Since the function call is scheduled on an executor, emit is always
    non-blocking.

    No effort is made to ensure thread safety, beyond using an executor.
    Nselfexecutorreturnc                    s(   t t|   |r|| _d S t | _d S N)superr   __init__	_executorr   )r   r   	__class__ A/var/www/html/myenv/lib/python3.10/site-packages/pyee/executor.pyr   1   s   
zExecutorEventEmitter.__init__fargs.kwargsc                    s:    j j|g|R i |}|jdtdd f fdd}d S )Nr   r   c                    s2   |   }t|tr d| d S |d ur|d S )Nerror)	exception
isinstance	Exceptionemit)r   excr   r   r   	_callback@   s   
z1ExecutorEventEmitter._emit_run.<locals>._callback)r   submitadd_done_callbackr   )r   r   r   r   futurer#   r   r"   r   	_emit_run8   s   zExecutorEventEmitter._emit_runTwaitc                 C   s   | j j|d dS )z)Call `shutdown` on the internal executor.)r(   N)r   shutdown)r   r(   r   r   r   r)   H   s   zExecutorEventEmitter.shutdownc                 C   s   | S r   r   r"   r   r   r   	__enter__M   s   zExecutorEventEmitter.__enter__typevalue	tracebackc                 C   s   |    d S r   )r)   )r   r+   r,   r-   r   r   r   __exit__P   s   zExecutorEventEmitter.__exit__r   )T)__name__
__module____qualname____doc__Selfr	   r   r   r   r
   r   r   strr'   boolr)   r*   r   r   r   r.   __classcell__r   r   r   r   r      s6     "


N)concurrent.futuresr   r   r   typesr   typingr   r   r   r	   r
   r   	pyee.baser   r3   __all__r   r   r   r   r   <module>   s    