pool.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. """Module implementing the Pool for :mod:``requests_toolbelt.threaded``."""
  2. import multiprocessing
  3. import requests
  4. from . import thread
  5. from .._compat import queue
  6. class Pool(object):
  7. """Pool that manages the threads containing sessions.
  8. :param queue:
  9. The queue you're expected to use to which you should add items.
  10. :type queue: queue.Queue
  11. :param initializer:
  12. Function used to initialize an instance of ``session``.
  13. :type initializer: collections.Callable
  14. :param auth_generator:
  15. Function used to generate new auth credentials for the session.
  16. :type auth_generator: collections.Callable
  17. :param int num_process:
  18. Number of threads to create.
  19. :param session:
  20. :type session: requests.Session
  21. """
  22. def __init__(self, job_queue, initializer=None, auth_generator=None,
  23. num_processes=None, session=requests.Session):
  24. if num_processes is None:
  25. num_processes = multiprocessing.cpu_count() or 1
  26. if num_processes < 1:
  27. raise ValueError("Number of processes should at least be 1.")
  28. self._job_queue = job_queue
  29. self._response_queue = queue.Queue()
  30. self._exc_queue = queue.Queue()
  31. self._processes = num_processes
  32. self._initializer = initializer or _identity
  33. self._auth = auth_generator or _identity
  34. self._session = session
  35. self._pool = [
  36. thread.SessionThread(self._new_session(), self._job_queue,
  37. self._response_queue, self._exc_queue)
  38. for _ in range(self._processes)
  39. ]
  40. def _new_session(self):
  41. return self._auth(self._initializer(self._session()))
  42. @classmethod
  43. def from_exceptions(cls, exceptions, **kwargs):
  44. r"""Create a :class:`~Pool` from an :class:`~ThreadException`\ s.
  45. Provided an iterable that provides :class:`~ThreadException` objects,
  46. this classmethod will generate a new pool to retry the requests that
  47. caused the exceptions.
  48. :param exceptions:
  49. Iterable that returns :class:`~ThreadException`
  50. :type exceptions: iterable
  51. :param kwargs:
  52. Keyword arguments passed to the :class:`~Pool` initializer.
  53. :returns: An initialized :class:`~Pool` object.
  54. :rtype: :class:`~Pool`
  55. """
  56. job_queue = queue.Queue()
  57. for exc in exceptions:
  58. job_queue.put(exc.request_kwargs)
  59. return cls(job_queue=job_queue, **kwargs)
  60. @classmethod
  61. def from_urls(cls, urls, request_kwargs=None, **kwargs):
  62. """Create a :class:`~Pool` from an iterable of URLs.
  63. :param urls:
  64. Iterable that returns URLs with which we create a pool.
  65. :type urls: iterable
  66. :param dict request_kwargs:
  67. Dictionary of other keyword arguments to provide to the request
  68. method.
  69. :param kwargs:
  70. Keyword arguments passed to the :class:`~Pool` initializer.
  71. :returns: An initialized :class:`~Pool` object.
  72. :rtype: :class:`~Pool`
  73. """
  74. request_dict = {'method': 'GET'}
  75. request_dict.update(request_kwargs or {})
  76. job_queue = queue.Queue()
  77. for url in urls:
  78. job = request_dict.copy()
  79. job.update({'url': url})
  80. job_queue.put(job)
  81. return cls(job_queue=job_queue, **kwargs)
  82. def exceptions(self):
  83. """Iterate over all the exceptions in the pool.
  84. :returns: Generator of :class:`~ThreadException`
  85. """
  86. while True:
  87. exc = self.get_exception()
  88. if exc is None:
  89. break
  90. yield exc
  91. def get_exception(self):
  92. """Get an exception from the pool.
  93. :rtype: :class:`~ThreadException`
  94. """
  95. try:
  96. (request, exc) = self._exc_queue.get_nowait()
  97. except queue.Empty:
  98. return None
  99. else:
  100. return ThreadException(request, exc)
  101. def get_response(self):
  102. """Get a response from the pool.
  103. :rtype: :class:`~ThreadResponse`
  104. """
  105. try:
  106. (request, response) = self._response_queue.get_nowait()
  107. except queue.Empty:
  108. return None
  109. else:
  110. return ThreadResponse(request, response)
  111. def responses(self):
  112. """Iterate over all the responses in the pool.
  113. :returns: Generator of :class:`~ThreadResponse`
  114. """
  115. while True:
  116. resp = self.get_response()
  117. if resp is None:
  118. break
  119. yield resp
  120. def join_all(self):
  121. """Join all the threads to the master thread."""
  122. for session_thread in self._pool:
  123. session_thread.join()
  124. class ThreadProxy(object):
  125. proxied_attr = None
  126. def __getattr__(self, attr):
  127. """Proxy attribute accesses to the proxied object."""
  128. get = object.__getattribute__
  129. if attr not in self.attrs:
  130. response = get(self, self.proxied_attr)
  131. return getattr(response, attr)
  132. else:
  133. return get(self, attr)
  134. class ThreadResponse(ThreadProxy):
  135. """A wrapper around a requests Response object.
  136. This will proxy most attribute access actions to the Response object. For
  137. example, if you wanted the parsed JSON from the response, you might do:
  138. .. code-block:: python
  139. thread_response = pool.get_response()
  140. json = thread_response.json()
  141. """
  142. proxied_attr = 'response'
  143. attrs = frozenset(['request_kwargs', 'response'])
  144. def __init__(self, request_kwargs, response):
  145. #: The original keyword arguments provided to the queue
  146. self.request_kwargs = request_kwargs
  147. #: The wrapped response
  148. self.response = response
  149. class ThreadException(ThreadProxy):
  150. """A wrapper around an exception raised during a request.
  151. This will proxy most attribute access actions to the exception object. For
  152. example, if you wanted the message from the exception, you might do:
  153. .. code-block:: python
  154. thread_exc = pool.get_exception()
  155. msg = thread_exc.message
  156. """
  157. proxied_attr = 'exception'
  158. attrs = frozenset(['request_kwargs', 'exception'])
  159. def __init__(self, request_kwargs, exception):
  160. #: The original keyword arguments provided to the queue
  161. self.request_kwargs = request_kwargs
  162. #: The captured and wrapped exception
  163. self.exception = exception
  164. def _identity(session_obj):
  165. return session_obj
  166. __all__ = ['ThreadException', 'ThreadResponse', 'Pool']