U
    ÎZjä  ã                   @   s‚   d Z ddlZddlZddlmZ ddlmZ G dd„ deƒZG d	d
„ d
eƒZ	G dd„ de	ƒZ
G dd„ de	ƒZdd„ ZdddgZdS )zEModule implementing the Pool for :mod:``requests_toolbelt.threaded``.é    Né   )Úthreadé   )Úqueuec                   @   sn   e Zd ZdZdddejfdd„Zdd„ Zedd„ ƒZ	edd	d
„ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú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_threads:
        Number of threads to create.
    :param session:
    :type session: requests.Session
    Nc                    s|   |d krt  ¡ pd}|dk r$tdƒ‚|ˆ _t ¡ ˆ _t ¡ ˆ _|ˆ _|pJt	ˆ _
|pTt	ˆ _|ˆ _‡ fdd„tˆ jƒD ƒˆ _d S )Nr   z)Number of processes should at least be 1.c                    s&   g | ]}t  ˆ  ¡ ˆ jˆ jˆ j¡‘qS © )r   ZSessionThreadÚ_new_sessionÚ
_job_queueÚ_response_queueÚ
_exc_queue)Ú.0Ú_©Úselfr   úb/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/requests_toolbelt/threaded/pool.pyÚ
<listcomp>*   s
   þ ÿz!Pool.__init__.<locals>.<listcomp>)ÚmultiprocessingÚ	cpu_countÚ
ValueErrorr	   r   ÚQueuer
   r   Z
_processesÚ	_identityÚ_initializerÚ_authÚ_sessionÚrangeÚ_pool)r   Ú	job_queueZinitializerZauth_generatorZnum_processesÚsessionr   r   r   Ú__init__   s    




ýzPool.__init__c                 C   s   |   |  |  ¡ ¡¡S ©N)r   r   r   r   r   r   r   r   0   s    zPool._new_sessionc                 K   s0   t  ¡ }|D ]}| |j¡ q| f 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   Úexcr   r   r   Úfrom_exceptions3   s    zPool.from_exceptionsc                 K   sZ   ddi}|  |pi ¡ t ¡ }|D ]$}| ¡ }|  d|i¡ | |¡ q"| f 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   )Úupdater   r   Úcopyr    )r"   Úurlsr!   r$   Zrequest_dictr   r)   Zjobr   r   r   Ú	from_urlsI   s    zPool.from_urlsc                 c   s   |   ¡ }|dkrq|V  q dS )zoIterate over all the exceptions in the pool.

        :returns: Generator of :class:`~ThreadException`
        N)Úget_exception)r   r%   r   r   r   r#   b   s    zPool.exceptionsc                 C   s:   z| j  ¡ \}}W n tjk
r*   Y dS X t||ƒS dS )zSGet an exception from the pool.

        :rtype: :class:`~ThreadException`
        N)r   Ú
get_nowaitr   ÚEmptyÚThreadException)r   Úrequestr%   r   r   r   r.   m   s
    zPool.get_exceptionc                 C   s:   z| j  ¡ \}}W n tjk
r*   Y dS X t||ƒS dS )zPGet a response from the pool.

        :rtype: :class:`~ThreadResponse`
        N)r
   r/   r   r0   ÚThreadResponse)r   r2   Úresponser   r   r   Úget_responsey   s
    zPool.get_responsec                 c   s   |   ¡ }|dkrq|V  q dS )zmIterate over all the responses in the pool.

        :returns: Generator of :class:`~ThreadResponse`
        N)r5   )r   Úrespr   r   r   Ú	responses…   s    zPool.responsesc                 C   s   | j D ]}| ¡  qdS )z*Join all the threads to the master thread.N)r   Újoin)r   Zsession_threadr   r   r   Újoin_all�   s    
zPool.join_all)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚrequestsÚSessionr   r   Úclassmethodr&   r-   r#   r.   r5   r7   r9   r   r   r   r   r   	   s    ÿ

r   c                   @   s   e Zd ZdZdd„ ZdS )ÚThreadProxyNc                 C   s4   t j}|| jkr&|| | jƒ}t||ƒS || |ƒS dS )z/Proxy attribute accesses to the proxied object.N)ÚobjectÚ__getattribute__ÚattrsÚproxied_attrÚgetattr)r   ÚattrÚgetr4   r   r   r   Ú__getattr__™   s
    

zThreadProxy.__getattr__)r:   r;   r<   rE   rI   r   r   r   r   rA   –   s   rA   c                   @   s(   e Zd ZdZdZeddgƒZdd„ ZdS )r3   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()

    r4   r!   c                 C   s   || _ || _d S r   )r!   r4   )r   r!   r4   r   r   r   r   ²   s    zThreadResponse.__init__N©r:   r;   r<   r=   rE   Ú	frozensetrD   r   r   r   r   r   r3   £   s   r3   c                   @   s(   e Zd ZdZdZeddgƒZdd„ ZdS )r1   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                 C   s   || _ || _d S r   )r!   rL   )r   r!   rL   r   r   r   r   È   s    zThreadException.__init__NrJ   r   r   r   r   r1   ¹   s   r1   c                 C   s   | S r   r   )Zsession_objr   r   r   r   Ï   s    r   )r=   r   r>   Ú r   Z_compatr   rB   r   rA   r3   r1   r   Ú__all__r   r   r   r   Ú<module>   s    