METADATA 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. Metadata-Version: 2.1
  2. Name: pika
  3. Version: 1.0.1
  4. Summary: Pika Python AMQP Client Library
  5. Home-page: https://pika.readthedocs.io
  6. Maintainer: Gavin M. Roy
  7. Maintainer-email: gavinmroy@gmail.com
  8. License: BSD
  9. Platform: UNKNOWN
  10. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: Intended Audience :: Developers
  12. Classifier: License :: OSI Approved :: BSD License
  13. Classifier: Natural Language :: English
  14. Classifier: Operating System :: OS Independent
  15. Classifier: Programming Language :: Python :: 2
  16. Classifier: Programming Language :: Python :: 2.7
  17. Classifier: Programming Language :: Python :: 3
  18. Classifier: Programming Language :: Python :: 3.4
  19. Classifier: Programming Language :: Python :: 3.5
  20. Classifier: Programming Language :: Python :: 3.6
  21. Classifier: Programming Language :: Python :: 3.7
  22. Classifier: Programming Language :: Python :: Implementation :: CPython
  23. Classifier: Programming Language :: Python :: Implementation :: Jython
  24. Classifier: Programming Language :: Python :: Implementation :: PyPy
  25. Classifier: Topic :: Communications
  26. Classifier: Topic :: Internet
  27. Classifier: Topic :: Software Development :: Libraries
  28. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  29. Classifier: Topic :: System :: Networking
  30. Provides-Extra: tornado
  31. Requires-Dist: tornado ; extra == 'tornado'
  32. Provides-Extra: twisted
  33. Requires-Dist: twisted ; extra == 'twisted'
  34. Pika
  35. ====
  36. Pika is a RabbitMQ (AMQP 0-9-1) client library for Python.
  37. |Version| |Python versions| |Status| |Coverage| |License| |Docs|
  38. Introduction
  39. ------------
  40. Pika is a pure-Python implementation of the AMQP 0-9-1 protocol including
  41. RabbitMQ's extensions.
  42. - Python 2.7 and 3.4+ are supported.
  43. - Since threads aren't appropriate to every situation, it doesn't require
  44. threads. Pika core takes care not to forbid them, either. The same goes for
  45. greenlets, callbacks, continuations, and generators. An instance of Pika's
  46. built-in connection adapters isn't thread-safe, however.
  47. - People may be using direct sockets, plain old ``select()``, or any of the
  48. wide variety of ways of getting network events to and from a Python
  49. application. Pika tries to stay compatible with all of these, and to make
  50. adapting it to a new environment as simple as possible.
  51. Documentation
  52. -------------
  53. Pika's documentation can be found at https://pika.readthedocs.io.
  54. Example
  55. -------
  56. Here is the most simple example of use, sending a message with the
  57. ``pika.BlockingConnection`` adapter:
  58. .. code :: python
  59. import pika
  60. connection = pika.BlockingConnection()
  61. channel = connection.channel()
  62. channel.basic_publish(exchange='test', routing_key='test',
  63. body=b'Test message.')
  64. connection.close()
  65. And an example of writing a blocking consumer:
  66. .. code :: python
  67. import pika
  68. connection = pika.BlockingConnection()
  69. channel = connection.channel()
  70. for method_frame, properties, body in channel.consume('test'):
  71. # Display the message parts and acknowledge the message
  72. print(method_frame, properties, body)
  73. channel.basic_ack(method_frame.delivery_tag)
  74. # Escape out of the loop after 10 messages
  75. if method_frame.delivery_tag == 10:
  76. break
  77. # Cancel the consumer and return any pending messages
  78. requeued_messages = channel.cancel()
  79. print('Requeued %i messages' % requeued_messages)
  80. connection.close()
  81. Pika provides the following adapters
  82. ------------------------------------
  83. - ``pika.adapters.asyncio_connection.AsyncioConnection`` - asynchronous adapter
  84. for Python 3 `AsyncIO <https://docs.python.org/3/library/asyncio.html>`_'s
  85. I/O loop.
  86. - ``pika.BlockingConnection`` - synchronous adapter on top of library for
  87. simple usage.
  88. - ``pika.SelectConnection`` - asynchronous adapter without third-party
  89. dependencies.
  90. - ``pika.adapters.tornado_connection.TornadoConnection`` - asynchronous adapter
  91. for use with `Tornado <http://tornadoweb.org>`_'s I/O loop.
  92. - ``pika.adapters.twisted_connection.TwistedProtocolConnection`` - asynchronous
  93. adapter for use with `Twisted <http://twistedmatrix.com>`_'s I/O loop.
  94. Multiple connection parameters
  95. ------------------------------
  96. You can also pass multiple ``pika.ConnectionParameters`` instances for
  97. fault-tolerance as in the code snippet below (host names are just examples, of
  98. course). To enable retries, set ``connection_attempts`` and ``retry_delay`` as
  99. needed in the last ``pika.ConnectionParameters`` element of the sequence.
  100. Retries occur after connection attempts using all of the given connection
  101. parameters fail.
  102. .. code :: python
  103. import pika
  104. parameters = (
  105. pika.ConnectionParameters(host='rabbitmq.zone1.yourdomain.com'),
  106. pika.ConnectionParameters(host='rabbitmq.zone2.yourdomain.com',
  107. connection_attempts=5, retry_delay=1))
  108. connection = pika.BlockingConnection(parameters)
  109. With non-blocking adapters, such as ``pika.SelectConnection`` and
  110. ``pika.adapters.asyncio_connection.AsyncioConnection``, you can request a
  111. connection using multiple connection parameter instances via the connection
  112. adapter's ``create_connection()`` class method.
  113. Requesting message acknowledgements from another thread
  114. -------------------------------------------------------
  115. The single-threaded usage constraint of an individual Pika connection adapter
  116. instance may result in a dropped AMQP/stream connection due to AMQP heartbeat
  117. timeout in consumers that take a long time to process an incoming message. A
  118. common solution is to delegate processing of the incoming messages to another
  119. thread, while the connection adapter's thread continues to service its I/O
  120. loop's message pump, permitting AMQP heartbeats and other I/O to be serviced in
  121. a timely fashion.
  122. Messages processed in another thread may not be acknowledged directly from that
  123. thread, since all accesses to the connection adapter instance must be from a
  124. single thread—the thread that is running the adapter's I/O loop. However, this
  125. may be accomplished by requesting a callback to be executed in the adapter's
  126. I/O loop thread. For example, the callback function's implementation might look
  127. like this:
  128. .. code :: python
  129. def ack_message(channel, delivery_tag):
  130. """Note that `channel` must be the same Pika channel instance via which
  131. the message being acknowledged was retrieved (AMQP protocol constraint).
  132. """
  133. if channel.is_open:
  134. channel.basic_ack(delivery_tag)
  135. else:
  136. # Channel is already closed, so we can't acknowledge this message;
  137. # log and/or do something that makes sense for your app in this case.
  138. pass
  139. The code running in the other thread may request the ``ack_message()`` function
  140. to be executed in the connection adapter's I/O loop thread using an
  141. adapter-specific mechanism:
  142. - ``pika.BlockingConnection`` abstracts its I/O loop from the application and
  143. thus exposes ``pika.BlockingConnection.add_callback_threadsafe()``. Refer to
  144. this method's docstring for additional information. For example:
  145. .. code :: python
  146. connection.add_callback_threadsafe(functools.partial(ack_message, channel, delivery_tag))
  147. - When using a non-blocking connection adapter, such as
  148. ``pika.adapters.asyncio_connection.AsyncioConnection`` or
  149. ``pika.SelectConnection``, you use the underlying asynchronous framework's
  150. native API for requesting an I/O loop-bound callback from another thread. For
  151. example, ``pika.SelectConnection``'s I/O loop provides
  152. ``add_callback_threadsafe()``,
  153. ``pika.adapters.tornado_connection.TornadoConnection``'s I/O loop has
  154. ``add_callback()``, while
  155. ``pika.adapters.asyncio_connection.AsyncioConnection``'s I/O loop exposes
  156. ``call_soon_threadsafe()``.
  157. This threadsafe callback request mechanism may also be used to delegate
  158. publishing of messages, etc., from a background thread to the connection
  159. adapter's thread.
  160. Connection recovery
  161. -------------------
  162. Some RabbitMQ clients (Bunny, Java, .NET, Objective-C, Swift) provide a way to
  163. automatically recover connection, its channels and topology (e.g. queues,
  164. bindings and consumers) after a network failure. Others require connection
  165. recovery to be performed by the application code and strive to make it a
  166. straightforward process. Pika falls into the second category.
  167. Pika supports multiple connection adapters. They take different approaches to
  168. connection recovery.
  169. For ``pika.BlockingConnection`` adapter exception handling can be used to check
  170. for connection errors. Here is a very basic example:
  171. .. code :: python
  172. import pika
  173. while True:
  174. try:
  175. connection = pika.BlockingConnection()
  176. channel = connection.channel()
  177. channel.basic_consume('test', on_message_callback)
  178. channel.start_consuming()
  179. # Don't recover if connection was closed by broker
  180. except pika.exceptions.ConnectionClosedByBroker:
  181. break
  182. # Don't recover on channel errors
  183. except pika.exceptions.AMQPChannelError:
  184. break
  185. # Recover on all other connection errors
  186. except pika.exceptions.AMQPConnectionError:
  187. continue
  188. This example can be found in `examples/consume_recover.py`.
  189. Generic operation retry libraries such as
  190. `retry <https://github.com/invl/retry>`_ can be used. Decorators make it
  191. possible to configure some additional recovery behaviours, like delays between
  192. retries and limiting the number of retries:
  193. .. code :: python
  194. from retry import retry
  195. @retry(pika.exceptions.AMQPConnectionError, delay=5, jitter=(1, 3))
  196. def consume():
  197. connection = pika.BlockingConnection()
  198. channel = connection.channel()
  199. channel.basic_consume('test', on_message_callback)
  200. try:
  201. channel.start_consuming()
  202. # Don't recover connections closed by server
  203. except pika.exceptions.ConnectionClosedByBroker:
  204. pass
  205. consume()
  206. This example can be found in `examples/consume_recover_retry.py`.
  207. For asynchronous adapters, use ``on_close_callback`` to react to connection
  208. failure events. This callback can be used to clean up and recover the
  209. connection.
  210. An example of recovery using ``on_close_callback`` can be found in
  211. `examples/asynchronous_consumer_example.py`.
  212. Contributing
  213. ------------
  214. To contribute to Pika, please make sure that any new features or changes to
  215. existing functionality **include test coverage**.
  216. *Pull requests that add or change code without adequate test coverage will be
  217. rejected.*
  218. Additionally, please format your code using
  219. `Yapf <http://pypi.python.org/pypi/yapf>`_ with ``google`` style prior to
  220. issuing your pull request. *Note: only format those lines that you have changed
  221. in your pull request. If you format an entire file and change code outside of
  222. the scope of your PR, it will likely be rejected.*
  223. Extending to support additional I/O frameworks
  224. ----------------------------------------------
  225. New non-blocking adapters may be implemented in either of the following ways:
  226. - By subclassing ``pika.BaseConnection``, implementing its abstract method and
  227. passing its constructor an implementation of
  228. ``pika.adapters.utils.nbio_interface.AbstractIOServices``.
  229. ``pika.BaseConnection`` implements ``pika.connection.Connection``'s abstract
  230. methods, including internally-initiated connection logic. For examples, refer
  231. to the implementations of
  232. ``pika.adapters.asyncio_connection.AsyncioConnection`` and
  233. ``pika.adapters.tornado_connection.TornadoConnection``.
  234. - By subclassing ``pika.connection.Connection`` and implementing its abstract
  235. methods. This approach facilitates implementation of custom
  236. connection-establishment and transport mechanisms. For an example, refer to
  237. the implementation of
  238. ``pika.adapters.twisted_connection.TwistedProtocolConnection``.
  239. .. |Version| image:: https://img.shields.io/pypi/v/pika.svg?
  240. :target: http://badge.fury.io/py/pika
  241. .. |Python versions| image:: https://img.shields.io/pypi/pyversions/pika.svg
  242. :target: https://pypi.python.org/pypi/pika
  243. .. |Status| image:: https://img.shields.io/travis/pika/pika.svg?
  244. :target: https://travis-ci.org/pika/pika
  245. .. |Coverage| image:: https://img.shields.io/codecov/c/github/pika/pika.svg?
  246. :target: https://codecov.io/github/pika/pika?branch=master
  247. .. |License| image:: https://img.shields.io/pypi/l/pika.svg?
  248. :target: https://pika.readthedocs.io
  249. .. |Docs| image:: https://readthedocs.org/projects/pika/badge/?version=stable
  250. :target: https://pika.readthedocs.io
  251. :alt: Documentation Status