
    |h                         d Z ddlZddlZddlmZ ddlmZ  G d de      Z G d	 d
e      Z	 G d de	      Z
 G d de	      Zd Zg dZy)zEModule implementing the Pool for :mod:``requests_toolbelt.threaded``.    N   )thread   )queuec                   z    e Zd ZdZdddej
                  fdZd Zed        Z	edd       Z
d Zd Zd	 Zd
 Zd Zy)Poola>  Pool that manages the threads containing sessions.

    :param queue:
        The queue you're expected to use to which you should add items.
    :type queue: queue.Queue
    :param initializer:
        Function used to initialize an instance of ``session``.
    :type initializer: collections.Callable
    :param auth_generator:
        Function used to generate new auth credentials for the session.
    :type auth_generator: collections.Callable
    :param int num_process:
        Number of threads to create.
    :param session:
    :type session: requests.Session
    Nc           	          |t        j                         xs d}|dk  rt        d      || _        t	        j
                         | _        t	        j
                         | _        || _        |xs t        | _
        |xs t        | _        || _        t        | j                        D cg c]F  }t        j                  | j!                         | j                  | j                  | j                        H c}| _        y c c}w )Nr   z)Number of processes should at least be 1.)multiprocessing	cpu_count
ValueError
_job_queuer   Queue_response_queue
_exc_queue
_processes	_identity_initializer_auth_sessionranger   SessionThread_new_session_pool)self	job_queueinitializerauth_generatornum_processessession_s          ^/var/www/html/test/engine/venv/lib/python3.12/site-packages/requests_toolbelt/threaded/pool.py__init__zPool.__init__   s     +557<1M1HII#${{}++-''49#0y
 4??+
    !2!2!4doo!%!5!5tH

 
s   'AC;c                 ^    | j                  | j                  | j                                     S N)r   r   r   )r   s    r!   r   zPool._new_session0   s"    zz$++DMMO<==    c                     t        j                         }|D ]  }|j                  |j                           | dd|i|S )a2  Create a :class:`~Pool` from an :class:`~ThreadException`\ s.

        Provided an iterable that provides :class:`~ThreadException` objects,
        this classmethod will generate a new pool to retry the requests that
        caused the exceptions.

        :param exceptions:
            Iterable that returns :class:`~ThreadException`
        :type exceptions: iterable
        :param kwargs:
            Keyword arguments passed to the :class:`~Pool` initializer.
        :returns: An initialized :class:`~Pool` object.
        :rtype: :class:`~Pool`
        r    )r   r   putrequest_kwargs)cls
exceptionskwargsr   excs        r!   from_exceptionszPool.from_exceptions3   sE      KKM	 	.CMM#,,-	. 1Y1&11r%   c                     ddi}|j                  |xs i        t        j                         }|D ]6  }|j                         }|j                  d|i       |j	                  |       8  | dd|i|S )a  Create a :class:`~Pool` from an iterable of URLs.

        :param urls:
            Iterable that returns URLs with which we create a pool.
        :type urls: iterable
        :param dict request_kwargs:
            Dictionary of other keyword arguments to provide to the request
            method.
        :param kwargs:
            Keyword arguments passed to the :class:`~Pool` initializer.
        :returns: An initialized :class:`~Pool` object.
        :rtype: :class:`~Pool`
        methodGETurlr   r'   )updater   r   copyr(   )r*   urlsr)   r,   request_dictr   r2   jobs           r!   	from_urlszPool.from_urlsI   sz     !%(N0b1KKM	 	C##%CJJs|$MM#	
 1Y1&11r%   c              #   <   K   	 | j                         }|y| w)zoIterate over all the exceptions in the pool.

        :returns: Generator of :class:`~ThreadException`
        N)get_exception)r   r-   s     r!   r+   zPool.exceptionsb   s+     
 $$&C{I	    c                     	 | j                   j                         \  }}t        ||      S # t        j                  $ r Y yw xY w)zSGet an exception from the pool.

        :rtype: :class:`~ThreadException`
        N)r   
get_nowaitThreadExceptionr   Empty)r   requestr-   s      r!   r:   zPool.get_exceptionm   sD    
	1!__779NWc #7C00 {{ 		   + A Ac                     	 | j                   j                         \  }}t        ||      S # t        j                  $ r Y yw xY w)zPGet a response from the pool.

        :rtype: :class:`~ThreadResponse`
        N)r   r=   ThreadResponser   r?   )r   r@   responses      r!   get_responsezPool.get_responsey   sG    
	5"&"6"6"A"A"CWh "'844 {{ 		rA   c              #   <   K   	 | j                         }|y| w)zmIterate over all the responses in the pool.

        :returns: Generator of :class:`~ThreadResponse`
        N)rE   )r   resps     r!   	responseszPool.responses   s+     
 $$&D|J	 r;   c                 F    | j                   D ]  }|j                           y)z*Join all the threads to the master thread.N)r   join)r   session_threads     r!   join_allzPool.join_all   s!    "jj 	"N!	"r%   r$   )__name__
__module____qualname____doc__requestsSessionr"   r   classmethodr.   r8   r+   r:   rE   rH   rL   r'   r%   r!   r   r   	   sd    " /34#X-=-=
*> 2 2* 2 20	
1
5	"r%   r   c                       e Zd ZdZd Zy)ThreadProxyNc                     t         j                  }|| j                  vr || | j                        }t	        ||      S  || |      S )z/Proxy attribute accesses to the proxied object.)object__getattribute__attrsproxied_attrgetattr)r   attrgetrD   s       r!   __getattr__zThreadProxy.__getattr__   sD    %%tzz!4!2!23H8T**tT?"r%   )rM   rN   rO   rZ   r^   r'   r%   r!   rU   rU      s    L#r%   rU   c                   .    e Zd ZdZdZ eddg      Zd Zy)rC   a>  A wrapper around a requests Response object.

    This will proxy most attribute access actions to the Response object. For
    example, if you wanted the parsed JSON from the response, you might do:

    .. code-block:: python

        thread_response = pool.get_response()
        json = thread_response.json()

    rD   r)   c                      || _         || _        y r$   )r)   rD   )r   r)   rD   s      r!   r"   zThreadResponse.__init__   s    , r%   NrM   rN   rO   rP   rZ   	frozensetrY   r"   r'   r%   r!   rC   rC      s#    
 L'45E!r%   rC   c                   .    e Zd ZdZdZ eddg      Zd Zy)r>   a=  A wrapper around an exception raised during a request.

    This will proxy most attribute access actions to the exception object. For
    example, if you wanted the message from the exception, you might do:

    .. code-block:: python

        thread_exc = pool.get_exception()
        msg = thread_exc.message

    	exceptionr)   c                      || _         || _        y r$   )r)   rd   )r   r)   rd   s      r!   r"   zThreadException.__init__   s    ,"r%   Nra   r'   r%   r!   r>   r>      s#    
 L'56E#r%   r>   c                     | S r$   r'   )session_objs    r!   r   r      s    r%   )r>   rC   r   )rP   r
   rQ    r   _compatr   rW   r   rU   rC   r>   r   __all__r'   r%   r!   <module>rk      sS    K    J"6 J"Z
#& 
#![ !,#k #, 8r%   