o
    Zh                     @   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g d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_process:
        Number of threads to create.
    :param session:
    :type session: requests.Session
    Nc                    s|   |d u r
t  p	d}|dk rtd| _t  _t  _| _|p%t	 _
|p*t	 _| _ 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   V/var/www/html/lang_env/lib/python3.10/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| 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   Nr   )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| 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   Nr   )updater   r   copyr    )r"   urlsr!   r$   Zrequest_dictr   r)   Zjobr   r   r   	from_urlsI   s   zPool.from_urlsc                 c        	 |   }|du rdS |V  q)zoIterate over all the exceptions in the pool.

        :returns: Generator of :class:`~ThreadException`
        TN)get_exception)r   r%   r   r   r   r#   b      zPool.exceptionsc                 C   4   z	| j  \}}W n tjy   Y dS w t||S )zSGet an exception from the pool.

        :rtype: :class:`~ThreadException`
        N)r   
get_nowaitr   EmptyThreadException)r   requestr%   r   r   r   r/   m      
zPool.get_exceptionc                 C   r1   )zPGet a response from the pool.

        :rtype: :class:`~ThreadResponse`
        N)r
   r2   r   r3   ThreadResponse)r   r5   responser   r   r   get_responsey   r6   zPool.get_responsec                 c   r.   )zmIterate over all the responses in the pool.

        :returns: Generator of :class:`~ThreadResponse`
        TN)r9   )r   respr   r   r   	responses   r0   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_allr   )__name__
__module____qualname____doc__requestsSessionr   r   classmethodr&   r-   r#   r/   r9   r;   r=   r   r   r   r   r   	   s    

r   c                   @   s   e Zd ZdZdd ZdS )ThreadProxyNc                 C   s0   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getr8   r   r   r   __getattr__   s
   


zThreadProxy.__getattr__)r>   r?   r@   rI   rM   r   r   r   r   rE      s    rE   c                   @   (   e Zd ZdZdZeddgZdd ZdS )r7   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()

    r8   r!   c                 C      || _ || _d S r   )r!   r8   )r   r!   r8   r   r   r   r         
zThreadResponse.__init__Nr>   r?   r@   rA   rI   	frozensetrH   r   r   r   r   r   r7      
    r7   c                   @   rN   )r4   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   rO   r   )r!   rT   )r   r!   rT   r   r   r   r      rP   zThreadException.__init__NrQ   r   r   r   r   r4      rS   r4   c                 C   s   | S r   r   )Zsession_objr   r   r   r      s   r   )r4   r7   r   )rA   r   rB    r   _compatr   rF   r   rE   r7   r4   r   __all__r   r   r   r   <module>   s     