o
    @BiR#                     @   sh   d dl mZ d dlZd dlZd dlZe  d dlmZ d dlm	Z	 dZ
dd Zdd	d
ZdddZdS )    )absolute_importN)reflect)ReactorAlreadyInstalledError)install_optimal_reactorinstall_reactorcurrent_reactor_klassc                  C   s2   dt jv rtt jd jdd } | S d} | S )zK
    Return class name of currently installed Twisted reactor or None.
    ztwisted.internet.reactor.N)sysmodulesr   qual	__class__split)current_reactor r   ^/var/www/html/Trade-python/venv/lib/python3.10/site-packages/autobahn/twisted/choosereactor.pyr   -   s
   
r   Tc                 C   sJ  t  }t }dtjv stjdrN|dkrH|du r;zddlm} |  W n   |j	dt
 d Y n|d	 n|j	d
|d | rGt n|d ntjdv r|dkr|du r|zddlm} |  W n   |j	dt
 d Y n|d n|j	d|d | rt n|d ntjdr|dkr|du rzddlm} |  W n   |j	dt
 d Y na|d n[|j	d|d | rt nN|d nH|dkr|du rzddlm} |  W n   |j	dt
 tjd Y n!|jdtjd  n|j	d!|tjd" | rt n|jd#tjd  ddlm} |t j_|S )$a  
    Try to install the optimal Twisted reactor for this platform:

    - Linux:   epoll
    - BSD/OSX: kqueue
    - Windows: iocp
    - Other:   select

    Notes:

    - This function exists, because the reactor types selected based on platform
      in `twisted.internet.default` are different from here.
    - The imports are inlined, because the Twisted code base is notorious for
      importing the reactor as a side-effect of merely importing. Hence we postpone
      all importing.

    See: http://twistedmatrix.com/documents/current/core/howto/choosing-reactor.html#reactor-functionality

    :param require_optimal_reactor: If ``True`` and the desired reactor could not be installed,
        raise ``ReactorAlreadyInstalledError``, else fallback to another reactor.
    :type require_optimal_reactor: bool

    :returns: The Twisted reactor in place (`twisted.internet.reactor`).
    bsddarwinKQueueReactorNr   )	kqreactorzJRunning on *BSD or MacOSX, but cannot install kqueue Twisted reactor: {tb})tbzERunning on *BSD or MacOSX and optimal reactor (kqueue) was installed.z}Running on *BSD or MacOSX, but cannot install kqueue Twisted reactor, because another reactor ({klass}) is already installed.)klasszIRunning on *BSD or MacOSX and optimal reactor (kqueue) already installed.)win32IOCPReactorreactorzARunning on Windows, but cannot install IOCP Twisted reactor: {tb}z<Running on Windows and optimal reactor (ICOP) was installed.ztRunning on Windows, but cannot install IOCP Twisted reactor, because another reactor ({klass}) is already installed.z@Running on Windows and optimal reactor (ICOP) already installed.linuxEPollReactor)epollreactorz@Running on Linux, but cannot install Epoll Twisted reactor: {tb}z;Running on Linux and optimal reactor (epoll) was installed.zsRunning on Linux, but cannot install Epoll Twisted reactor, because another reactor ({klass}) is already installed.z?Running on Linux and optimal reactor (epoll) already installed.SelectReactor)selectreactorzHRunning on "{platform}", but cannot install Select Twisted reactor: {tb})r   platformzCRunning on "{platform}" and optimal reactor (Select) was installed.)r!   z{Running on "{platform}", but cannot install Select Twisted reactor, because another reactor ({klass}) is already installed.)r   r!   zGRunning on "{platform}" and optimal reactor (Select) already installed.)txaiomake_loggerr   r
   r!   
startswithtwisted.internetr   installwarn	traceback
format_excdebugr   twisted.internet.iocpreactorr   r   r    configloop)require_optimal_reactorlogr   r   iocpreactorr   r    r   r   r   r   r   8   sx   


r   Fc                 C   s   |st  }| r/ddlm} |r|jd| d z||  W n   |jd| d td Y n|r6|d t| ddl	m
} |t j_|rVddlm} |jd	||jd |S )
a  
    Install Twisted reactor.

    :param explicit_reactor: If provided, install this reactor. Else, install
        the optimal reactor.
    :type explicit_reactor: obj

    :param verbose: If ``True``, log (at level "info") the reactor that is
        in place afterwards.
    :type verbose: bool

    :param log: Explicit logging to this txaio logger object.
    :type log: obj

    :param require_optimal_reactor: If ``True`` and the desired reactor could not be installed,
        raise ``ReactorAlreadyInstalledError``, else fallback to another reactor.
    :type require_optimal_reactor: bool

    :returns: The Twisted reactor in place (`twisted.internet.reactor`).
    r   )installReactorzETrying to install explicitly specified Twisted reactor "{reactor}" ..r   z?Could not install Twisted reactor {reactor}
{log_failure.value}   z1Automatically choosing optimal Twisted reactor ..)r   z$Running on Twisted reactor {reactor})r"   r#   twisted.application.reactorsr1   infofailurer
   exitr   r%   r   r,   r-   twisted.python.reflectr   r   )explicit_reactorverboser/   r.   r1   r   r   r   r   r   r      s,   
r   )T)NFNT)
__future__r   r
   r(   r"   use_twistedtwisted.pythonr   twisted.internet.errorr   __all__r   r   r   r   r   r   r   <module>   s   
y