a *Na@sdZddlZddlZddlmZddlmZGdddeZGd d d eZ Gd d d e Z Gd dde Z ddZ gdZ dS)zEModule implementing the Pool for :mod:``requests_toolbelt.threaded``.N)thread)queuec@sneZdZdZdddejfddZddZeddZ edd d Z d d Z d dZ ddZ ddZddZdS)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 Ncs||durtpd}|dkr$td|_t_t_|_|pJt _ |pTt _ |_ fddt jD_dS)Nrz)Number of processes should at least be 1.cs&g|]}tjjjqS)rZ SessionThread _new_session _job_queue_response_queue _exc_queue).0_selfrz/private/var/folders/js/6pj4vh5d4zd0k6bxv74qrbhr0000gr/T/pip-target-22xwyzbs/lib/python/requests_toolbelt/threaded/pool.py *sz!Pool.__init__..)multiprocessing cpu_count ValueErrorr rQueuer r Z _processes _identity _initializer_auth_sessionrange_pool)r job_queueZ initializerZauth_generatorZ num_processessessionrrr__init__s      z Pool.__init__cCs|||SN)rrrrrrrr0szPool._new_sessioncKs0t}|D]}||jq |fd|i|S)a2Create 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)rrputrequest_kwargs)cls exceptionskwargsrexcrrrfrom_exceptions3szPool.from_exceptionscKsZddi}||pit}|D]$}|}|d|i||q"|fd|i|S)aCreate 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` methodGETurlr)updaterrcopyr )r"urlsr!r$Z request_dictrr)Zjobrrr from_urlsIs zPool.from_urlsccs|}|durq|VqdS)zoIterate over all the exceptions in the pool. :returns: Generator of :class:`~ThreadException` N) get_exception)rr%rrrr#bszPool.exceptionscCs8z|j\}}Wntjy(YdS0t||SdS)zSGet an exception from the pool. :rtype: :class:`~ThreadException` N)r get_nowaitrEmptyThreadException)rrequestr%rrrr.ms zPool.get_exceptioncCs8z|j\}}Wntjy(YdS0t||SdS)zPGet a response from the pool. :rtype: :class:`~ThreadResponse` N)r r/rr0ThreadResponse)rr2responserrr get_responseys zPool.get_responseccs|}|durq|VqdS)zmIterate over all the responses in the pool. :returns: Generator of :class:`~ThreadResponse` N)r5)rresprrr responsesszPool.responsescCs|jD] }|qdS)z*Join all the threads to the master thread.N)rjoin)rZsession_threadrrrjoin_alls z Pool.join_all)N)__name__ __module__ __qualname____doc__requestsSessionrr classmethodr&r-r#r.r5r7r9rrrrr s       rc@seZdZdZddZdS) ThreadProxyNcCs4tj}||jvr&|||j}t||S|||SdS)z/Proxy attribute accesses to the proxied object.N)object__getattribute__attrs proxied_attrgetattr)rattrgetr4rrr __getattr__s    zThreadProxy.__getattr__)r:r;r<rErIrrrrrAsrAc@s(eZdZdZdZeddgZddZdS)r3a>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() r4r!cCs||_||_dSr)r!r4)rr!r4rrrrszThreadResponse.__init__Nr:r;r<r=rE frozensetrDrrrrrr3s  r3c@s(eZdZdZdZeddgZddZdS)r1a=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!cCs||_||_dSr)r!rL)rr!rLrrrrszThreadException.__init__NrJrrrrr1s  r1cCs|Srr)Z session_objrrrrsr)r1r3r)r=rr>r_compatrrBrrAr3r1r__all__rrrrs