kombu/docs/userguide/connections.rst

201 lines
6.7 KiB
ReStructuredText
Raw Normal View History

2011-09-11 14:05:08 +00:00
.. _guide-connections:
============================
Connections and transports
============================
2011-09-11 14:05:08 +00:00
.. _connection-basics:
Basics
======
To send and receive messages you need a transport and a connection.
2014-03-17 21:06:12 +00:00
There are several transports to choose from (amqp, librabbitmq, redis, qpid, in-memory, etc.),
and you can even create your own. The default transport is amqp.
2016-04-07 22:59:26 +00:00
Create a connection using the default transport:
.. code-block:: pycon
>>> from kombu import Connection
>>> connection = Connection('amqp://guest:guest@localhost:5672//')
The connection will not be established yet, as the connection is established
when needed. If you want to explicitly establish the connection
you have to call the :meth:`~kombu.Connection.connect`
2016-04-07 22:59:26 +00:00
method:
.. code-block:: pycon
>>> connection.connect()
2016-04-07 22:59:26 +00:00
You can also check whether the connection is connected:
.. code-block:: pycon
2011-09-11 14:05:08 +00:00
>>> connection.connected
2011-09-11 14:05:08 +00:00
True
2016-04-07 22:59:26 +00:00
Connections must always be closed after use:
.. code-block:: pycon
2011-09-11 14:05:08 +00:00
>>> connection.close()
But best practice is to release the connection instead,
this will release the resource if the connection is associated
with a connection pool, or close the connection if not,
2016-04-07 22:59:26 +00:00
and makes it easier to do the transition to connection pools later:
.. code-block:: pycon
2011-09-11 14:05:08 +00:00
>>> connection.release()
.. seealso::
:ref:`guide-pools`
Of course, the connection can be used as a context, and you are
encouraged to do so as it makes it harder to forget releasing open
2016-04-07 22:59:26 +00:00
resources:
.. code-block:: python
2011-09-11 14:05:08 +00:00
with Connection() as connection:
2011-09-11 14:05:08 +00:00
# work with connection
.. _connection-urls:
URLs
====
2016-05-13 01:51:55 +00:00
Connection parameters can be provided as a URL in the format:
2016-04-07 22:59:26 +00:00
.. code-block:: text
2011-09-11 14:05:08 +00:00
transport://userid:password@hostname:port/virtual_host
2016-04-07 22:59:26 +00:00
All of these are valid URLs:
.. code-block:: text
2011-09-11 14:05:08 +00:00
# Specifies using the amqp transport only, default values
# are taken from the keyword arguments.
amqp://
# Using Redis
redis://localhost:6379/
# Using Redis over a Unix socket
redis+socket:///tmp/redis.sock
2014-03-17 21:06:12 +00:00
# Using Qpid
qpid://localhost/
2011-09-11 14:05:08 +00:00
# Using virtual host '/foo'
amqp://localhost//foo
# Using virtual host 'foo'
amqp://localhost/foo
2016-04-07 22:59:26 +00:00
The query part of the URL can also be used to set options, e.g.:
.. code-block:: text
2011-09-11 14:05:08 +00:00
amqp://localhost/myvhost?ssl=1
See :ref:`connection-options` for a list of supported options.
A connection without options will use the default connection settings,
2011-11-02 02:39:48 +00:00
which is using the localhost host, default port, user name `guest`,
2010-10-27 07:17:37 +00:00
password `guest` and virtual host "/". A connection without arguments
2016-04-07 22:59:26 +00:00
is the same as:
.. code-block:: pycon
>>> Connection('amqp://guest:guest@localhost:5672//')
2011-09-11 14:05:08 +00:00
The default port is transport specific, for AMQP this is 5672.
Other fields may also have different meaning depending on the transport
2010-10-27 07:17:37 +00:00
used. For example, the Redis transport uses the `virtual_host` argument as
the redis database number.
2011-09-11 14:05:08 +00:00
.. _connection-options:
Keyword arguments
=================
The :class:`~kombu.Connection` class supports additional
2011-09-11 14:05:08 +00:00
keyword arguments, these are:
2011-11-02 02:39:48 +00:00
:hostname: Default host name if not provided in the URL.
:userid: Default user name if not provided in the URL.
2011-09-11 14:05:08 +00:00
:password: Default password if not provided in the URL.
:virtual_host: Default virtual host if not provided in the URL.
:port: Default port if not provided in the URL.
:transport: Default transport if not provided in the URL.
Can be a string specifying the path to the class. (e.g.
``kombu.transport.pyamqp:Transport``), or one of the aliases:
2014-03-17 21:06:12 +00:00
``pyamqp``, ``librabbitmq``, ``redis``, ``qpid``, ``memory``, and so on.
2011-09-11 14:05:08 +00:00
2011-11-02 02:39:48 +00:00
:ssl: Use SSL to connect to the server. Default is ``False``.
2014-03-17 21:06:12 +00:00
Only supported by the amqp and qpid transports.
2011-09-11 14:05:08 +00:00
:insist: Insist on connecting to a server.
*No longer supported, relic from AMQP 0.8*
2011-09-11 14:05:08 +00:00
:connect_timeout: Timeout in seconds for connecting to the
2011-11-02 02:39:48 +00:00
server. May not be supported by the specified transport.
2011-09-11 14:05:08 +00:00
:transport_options: A dict of additional connection arguments to
pass to alternate kombu channel implementations. Consult the transport
2011-09-11 14:05:08 +00:00
documentation for available options.
AMQP Transports
===============
2014-03-17 21:06:12 +00:00
There are 4 transports available for AMQP use.
1. ``pyamqp`` uses the pure Python library ``amqp``, automatically
installed with Kombu.
2. ``librabbitmq`` uses the high performance transport written in C.
This requires the ``librabbitmq`` Python package to be installed, which
automatically compiles the C library.
3. ``amqp`` tries to use ``librabbitmq`` but falls back to ``pyamqp``.
2014-03-17 21:06:12 +00:00
4. ``qpid`` uses the pure Python library ``qpid.messaging``, automatically
installed with Kombu. The Qpid library uses AMQP, but uses custom
2014-03-17 21:06:12 +00:00
extensions specifically supported by the Apache Qpid Broker.
For the highest performance, you should install the ``librabbitmq`` package.
To ensure librabbitmq is used, you can explicitly specify it in the
transport URL, or use ``amqp`` to have the fallback.
2011-09-11 14:05:08 +00:00
Transport Comparison
====================
+---------------+----------+------------+------------+---------------+--------------+
| **Client** | **Type** | **Direct** | **Topic** | **Fanout** | **Priority** |
+---------------+----------+------------+------------+---------------+--------------+
| *amqp* | Native | Yes | Yes | Yes | Yes [#f3]_ |
+---------------+----------+------------+------------+---------------+--------------+
| *qpid* | Native | Yes | Yes | Yes | No |
+---------------+----------+------------+------------+---------------+--------------+
| *redis* | Virtual | Yes | Yes | Yes (PUB/SUB) | Yes |
+---------------+----------+------------+------------+---------------+--------------+
| *SQS* | Virtual | Yes | Yes [#f1]_ | Yes [#f2]_ | No |
+---------------+----------+------------+------------+---------------+--------------+
| *zookeeper* | Virtual | Yes | Yes [#f1]_ | No | Yes |
+---------------+----------+------------+------------+---------------+--------------+
| *in-memory* | Virtual | Yes | Yes [#f1]_ | No | No |
+---------------+----------+------------+------------+---------------+--------------+
| *SLMQ* | Virtual | Yes | Yes [#f1]_ | No | No |
+---------------+----------+------------+------------+---------------+--------------+
2012-01-20 14:21:03 +00:00
.. [#f1] Declarations only kept in memory, so exchanges/queues
must be declared by all clients that needs them.
.. [#f2] Fanout supported via storing routing tables in SimpleDB.
Disabled by default, but can be enabled by using the
``supports_fanout`` transport option.
.. [#f3] AMQP Message priority support depends on broker implementation.