a
    ^g                     @   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  _| _|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   m/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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u 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   s8   z| j  \}}W n tjy(   Y dS 0 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   s8   z| j  \}}W n tjy(   Y dS 0 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u 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v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   )r1   r3   r   )r=   r   r>    r   Z_compatr   rB   r   rA   r3   r1   r   __all__r   r   r   r   <module>   s    