
    㾙h	                     p    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e      Zy)    )ExecutorFutureThreadPoolExecutor)TracebackType)AnyCallableDictOptionalTupleType)EventEmitterExecutorEventEmitterc                        e Zd ZdZddedee   ddf fdZdedede	e
d	f   d
eee
f   ddf
dZddededdfdZdedd fdZdedee   dededee   f
dZ x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                 \    t         t        |           |r|| _        y t	               | _        y N)superr   __init__	_executorr   )r   r   	__class__s     I/var/www/Befach/backend/env/lib/python3.12/site-packages/pyee/executor.pyr   zExecutorEventEmitter.__init__1   s%    "D24'/DN/1DN    fargs.kwargsc                        j                   j                  |g|i |}|j                  dt        dd f fd       }y )Nr   r   c                 t    | j                         }t        |t              rj                  d|       y ||y )Nerror)	exception
isinstance	Exceptionemit)r   excr   s     r   	_callbackz1ExecutorEventEmitter._emit_run.<locals>._callback@   s5    +,;;=C#y)		'3'	 !r   )r   submitadd_done_callbackr   )r   r   r   r   futurer&   s   `     r   	_emit_runzExecutorEventEmitter._emit_run8   sK     /..qB4B6B		!	!	 	D 	 
"	r   waitc                 <    | j                   j                  |       y)z)Call `shutdown` on the internal executor.)r+   N)r   shutdown)r   r+   s     r   r-   zExecutorEventEmitter.shutdownH   s     	T*r   c                     | S r    )r   s    r   	__enter__zExecutorEventEmitter.__enter__M   s    r   typevalue	tracebackc                 $    | j                          y r   )r-   )r   r1   r2   r3   s       r   __exit__zExecutorEventEmitter.__exit__P   s     	r   r   )T)__name__
__module____qualname____doc__Selfr
   r   r   r   r   r   r	   strr*   boolr-   r0   r   r#   r   r5   __classcell__)r   s   @r   r   r      s     D2t 2x'9 2T 2 CHo S#X	
 
 +t +4 +4 +
 !7 y/2;HU	$r   N)concurrent.futuresr   r   r   typesr   typingr   r   r	   r
   r   r   	pyee.baser   r:   __all__r   r/   r   r   <module>rC      s5    D C  = = "
!
"F< Fr   