Ë
    ÜÍ:j  ã                   óÆ   — U d Z ddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
mZ  G d„ de«      Zi Zeeef   ed<   i Zeee
f   ed	<   i Zeeef   ed
<   ddœd„Zd„ Zd„ Zd„ Zy)a  Cross-process transport for callback messages.

When a callback is registered on an estimator that uses multiple worker processes, every
worker ends up with its own copy of the callback (sent there by pickling). However, the
user-visible state (e.g. logs being filled in or progress bars advancing) lives on the
main process. This module provides a way for the worker copies to ship their messages
back to the main process over a local endpoint (a UNIX socket on Unix, a Windows named
pipe on Windows).

Remark: we don't use a `multiprocessing.Manager` because its proxy objects become
unusable once the Manager subprocess is gone, e.g. after unpickling in a fresh
interpreter. The only ways to work around that either rely on multiprocessing private
API or give up cross-process capabilities.
é    N)ÚClientÚ
ConnectionÚListener)ÚThread)ÚCallableÚ
NamedTuplec                   ó&   — e Zd ZU dZeed<   eed<   y)ÚListenerHandlea•  A picklable reference to a main-process listener.

    Attributes
    ----------
    address : str
        Address of the local endpoint the listener is bound to.
        Workers use this to connect, and the main process also uses it as a key into
        this module's registries to find the live listener.

    authkey : bytes
        Shared secret used to authenticate connections to the listener.
    ÚaddressÚauthkeyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__Úbytes© ó    úp/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/sklearn/callback/_transport.pyr
   r
      s   … ñð ƒLØ„Nr   r
   Ú
_listenersÚ_message_consumersÚ_worker_connections)Úownerc                óP  ‡ ‡‡— t        j                  d«      }t        |d¬«      Št        ‰j                  |¬«      }‰t
        |j                  <   ‰ t        |j                  <   |�t        j                  |t        |«       ˆ fd„Šˆˆfd„}t        |d¬«      j                  «        |S )	aå  Create a listener for incoming messages on the main process.

    Also registers the listener and its message consumer in the module-level dicts.

    Parameters
    ----------
    message_consumer : callable
        A one-argument function, `message_consumer(message)`, that processes incoming
        message to update the callback's state. This callable may be called from
        multiple different threads, and must therefore behave in a thread-safe manner.

    owner : callback instance, default=None
        Optional owner callback. When provided, the listener is automatically closed
        when `owner` gets garbage-collected.

    Returns
    -------
    listener_handle : ListenerHandle
        A reference to the listener.
    é    é€   )r   Úbacklog)r   r   c                 ó‚   •— 	 	  ‰| j                  «       «       | j                  d «       Œ(# t        t        f$ r Y y w xY w)N)ÚrecvÚsendÚEOFErrorÚOSError)ÚconnÚmessage_consumers    €r   Ú_handlezopen_listener.<locals>._handlei   sB   ø€ ð	ØÙ  §¡£Ô-Ø—	‘	˜$”ð øô œ'Ð"ò 	Ùð	ús   ƒ), ¬>½>c                  ó‚   •— 	 	 ‰j                  «       } t        ‰| fd¬«      j                  «        Œ/# t        $ r Y y w xY w)NT)ÚtargetÚargsÚdaemon)Úacceptr#   r   Ústart)r$   r&   Úlisteners    €€r   Ú_acceptzopen_listener.<locals>._acceptt   sI   ø€ ØðØ—‘Ó(�ô ˜'¨¨¸Ô=×CÑCÔEð øô ò Ùðús   „2 ²	>½>T)r(   r*   )ÚosÚurandomr   r
   r   r   r   ÚweakrefÚfinalizeÚclose_listenerr   r,   )r%   r   r   Úlistener_handler.   r&   r-   s   `    @@r   Úopen_listenerr5   D   s‘   ú€ ô* �j‰j˜‹n€Gô  °Ô5€HÜ$¨X×-=Ñ-=ÀwÔO€Oà*2„Jˆ×&Ñ&Ñ'Ø2BÔ�×.Ñ.Ñ/ØÐÜ×Ñ˜¤°Ô@ô	õFô �' $Ô'×-Ñ-Ô/ØÐr   c                 óª   — t         j                  | j                  d«       t        j                  | j                  d«      }|�|j	                  «        yy)zEStop listening for `listener_handle` and free its background threads.N)r   Úpopr   r   Úclose)r4   r-   s     r   r3   r3   €   sC   € ä×Ñ˜?×2Ñ2°DÔ9Ü�~‰~˜o×5Ñ5°tÓ<€HØÐØ�‰Õð r   c                 ó¨   — | j                   t        v ry	 t        | j                   | j                  ¬«      j	                  «        y# t
        $ r Y yw xY w)a´  Whether the listener at `listener_handle` is usable from this process.

    Helper for callbacks that open their listener eagerly (e.g. in `__init__`) and
    therefore have to decide, on unpickling, whether to keep the inherited handle
    or open a fresh listener. The listener is not reusable when:

    - We are the process that originally opened the listener. Reusing the handle
      would route messages through the in-process fast path of `send`, into the
      original instance's message consumer instead of the unpickled instance's.

    - The listener is no longer reachable, e.g. unpickling in a fresh interpreter,
      or on a host that cannot reach the original listener.
    F©r   T)r   r   r   r   r8   r#   )r4   s    r   Úcan_reuse_listenerr;   ˆ   sR   € ð ×Ñ¤*Ñ,ØðÜˆ×&Ñ&°×0GÑ0GÔH×NÑNÔPð øô ò Ùðús   •/A Á	AÁAc                 ó   — t         j                  | j                  «      }|�	 ||«       y| j                  }t        j                  |«      }|€ t	        || j
                  ¬«      }|t        |<   |j                  |«       |j                  «        y)aä  Deliver `message` to whoever is listening on `listener_handle`.

    There are two possible delivery paths:

    - In-process fast path: `send` is called in the same process that called
      `open_listener` for this listener handle. The message consumer can directly be
      called without any serialization overhead.

    - Cross-process path: `send` is called in a different process. The worker opens
      a `Client` connection to the main-process listener on first use and caches it
      in `_worker_connections`, so all subsequent messages reuse the same socket.
      `send` then waits for an acknowledgement from the main process so that, by
      the time it returns, the message has actually been processed by the consumer.
    Nr:   )r   Úgetr   r   r   r   r!   r    )r4   Úmessager%   r   Ú
connections        r   r!   r!   Ÿ   s€   € ô *×-Ñ-¨o×.EÑ.EÓFÐØÐ#Ù˜Ô!Øà×%Ñ%€GÜ$×(Ñ(¨Ó1€JØÐÜ˜G¨_×-DÑ-DÔEˆ
Ø'1Ô˜GÑ$Ø‡O�O�GÔØ‡O�OÕr   )r   r/   r1   Úmultiprocessing.connectionr   r   r   Ú	threadingr   Útypingr   r   r
   r   Údictr   r   r   r   r5   r3   r;   r!   r   r   r   ú<module>rD      s„   ðòó 
Û ß CÑ CÝ ß 'ô�Zô ðB #%€
ˆD��h�ÑÓ $Ø*,Ð �D˜˜h˜Ñ'Ó ,ð
 .0Ð �T˜#˜z˜/Ñ*Ó /ð .2ô 9òxòó.r   