summaryrefslogtreecommitdiff
path: root/introduction.html
blob: a5ab8c536a6732a83fbc51345957c17c173199dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>kombu - AMQP Messaging Framework for Python &mdash; Kombu v0.1.0 documentation</title>
    <link rel="stylesheet" href="static/celery.css" type="text/css" />
    <link rel="stylesheet" href="static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="static/jquery.js"></script>
    <script type="text/javascript" src="static/underscore.js"></script>
    <script type="text/javascript" src="static/doctools.js"></script>
    <link rel="top" title="Kombu v0.1.0 documentation" href="index.html" />
    <link rel="next" title="Frequently Asked Questions" href="faq.html" />
    <link rel="prev" title="Kombu Documentation" href="index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="faq.html" title="Frequently Asked Questions"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Kombu Documentation"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Kombu v0.1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="kombu-amqp-messaging-framework-for-python">
<h1>kombu - AMQP Messaging Framework for Python<a class="headerlink" href="#kombu-amqp-messaging-framework-for-python" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Version:</th><td class="field-body">0.1.0</td>
</tr>
</tbody>
</table>
<p><strong>THIS IS A REWRITE OF CARROT</strong></p>
<p>Carrot will be discontinued in favor of Kombu.</p>
<p>Proposed API:</p>
<div class="highlight-python"><pre>from kombu.connection BrokerConnection
from kombu.messaging import Exchange, Queue, Consumer, Producer

media_exchange = Exchange("media", "direct", durable=True)
video_queue = Queue("video", exchange=media_exchange, key="video")

# connections/channels
connection = BrokerConnection("localhost", "guest", "guest", "/")
channel = connection.channel()

# produce
producer = Producer(channel, exchange=media_exchange, serializer="json")
producer.publish({"name": "/tmp/lolcat1.avi", "size": 1301013})

# consume
consumer = Consumer(channel, video_queue)
consumer.register_callback(process_media)
consumer.consume()

while True:
    connection.drain_events()


# consumerset:
video_queue = Queue("video", exchange=media_exchange, key="video")
image_queue = Queue("image", exchange=media_exchange, key="image")

consumer = Consumer(channel, [video_queue, image_queue])
consumer.consume()

while True:
    connection.drain_events()</pre>
</div>
<p>Exchanges/Queue can be bound to a channel:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">exchange</span> <span class="o">=</span> <span class="n">Exchange</span><span class="p">(</span><span class="s">&quot;tasks&quot;</span><span class="p">,</span> <span class="s">&quot;direct&quot;</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">connection</span> <span class="o">=</span> <span class="n">BrokerConnection</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">channel</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">channel</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bound_exchange</span> <span class="o">=</span> <span class="n">exchange</span><span class="p">(</span><span class="n">channel</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bound_exchange</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>

<span class="go"># the original exchange is not affected, and stays unbound.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exchange</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
<span class="go">raise NotBoundError: Can&#39;t call delete on Exchange not bound to</span>
<span class="go">    a channel.</span>
</pre></div>
</div>
<p><strong>ORIGINAL CARROT README BELOW</strong></p>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">kombu</span></tt> is an <a class="reference external" href="http://amqp.org">AMQP</a> messaging queue framework. AMQP is the Advanced Message
Queuing Protocol, an open standard protocol for message orientation, queuing,
routing, reliability and security.</p>
<p>The aim of <tt class="docutils literal"><span class="pre">kombu</span></tt> is to make messaging in Python as easy as possible by
providing a high-level interface for producing and consuming messages. At the
same time it is a goal to re-use what is already available as much as possible.</p>
<p><cite>kombu</cite> has pluggable messaging transports, so it is possible to support
several messaging systems. Currently, there is support for <a class="reference external" href="http://amqp.org">AMQP</a>
(<a class="reference external" href="http://barryp.org/software/py-amqplib/">py-amqplib</a>, <a class="reference external" href="http://github.com/tonyg/pika">pika</a>), <a class="reference external" href="http://stomp.codehaus.org">STOMP</a> (<a class="reference external" href="http://pypi.python.org/stompy">stompy</a>). There&#8217;s also an
in-memory transport for testing purposes, using the <a class="reference external" href="http://docs.python.org/library/queue.html">Python queue module</a>.</p>
<p>Several AMQP message broker implementations exists, including <a class="reference external" href="http://www.rabbitmq.com/">RabbitMQ</a>,
<a class="reference external" href="http://activemq.apache.org/">Apache ActiveMQ</a>. You&#8217;ll need to have one of these installed,
personally we&#8217;ve been using <a class="reference external" href="http://www.rabbitmq.com/">RabbitMQ</a>.</p>
<p>Before you start playing with <tt class="docutils literal"><span class="pre">kombu</span></tt>, you should probably read up on
AMQP, and you could start with the excellent article about using RabbitMQ
under Python, <a class="reference external" href="http://blogs.digitar.com/jjww/2009/01/rabbits-and-warrens/">Rabbits and warrens</a>. For more detailed information, you can
refer to the <a class="reference external" href="http://en.wikipedia.org/wiki/AMQP">Wikipedia article about AMQP</a>.</p>
</div>
<div class="section" id="documentation">
<h2>Documentation<a class="headerlink" href="#documentation" title="Permalink to this headline">¶</a></h2>
<p>Kombu is using Sphinx, and the latest documentation is available at GitHub:</p>
<blockquote>
<a class="reference external" href="http://ask.github.com/kombu">http://ask.github.com/kombu</a></blockquote>
<div class="section" id="installation">
<h3>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h3>
<p>You can install <tt class="docutils literal"><span class="pre">kombu</span></tt> either via the Python Package Index (PyPI)
or from source.</p>
<p>To install using <tt class="docutils literal"><span class="pre">pip</span></tt>,:</p>
<div class="highlight-python"><pre>$ pip install kombu</pre>
</div>
<p>To install using <tt class="docutils literal"><span class="pre">easy_install</span></tt>,:</p>
<div class="highlight-python"><pre>$ easy_install kombu</pre>
</div>
<p>If you have downloaded a source tarball you can install it
by doing the following,:</p>
<div class="highlight-python"><pre>$ python setup.py build
# python setup.py install # as root</pre>
</div>
</div>
<div class="section" id="terminology">
<h3>Terminology<a class="headerlink" href="#terminology" title="Permalink to this headline">¶</a></h3>
<p>There are some concepts you should be familiar with before starting:</p>
<blockquote>
<ul>
<li><p class="first">Producers</p>
<blockquote>
<p>Producers sends messages to an exchange.</p>
</blockquote>
</li>
<li><p class="first">Exchanges</p>
<blockquote>
<p>Messages are sent to exchanges. Exchanges are named and can be
configured to use one of several routing algorithms. The exchange
routes the messages to consumers by matching the routing key in the
message with the routing key the consumer provides when binding to
the exchange.</p>
</blockquote>
</li>
<li><p class="first">Consumers</p>
<blockquote>
<p>Consumers declares a queue, binds it to a exchange and receives
messages from it.</p>
</blockquote>
</li>
<li><p class="first">Queues</p>
<blockquote>
<p>Queues receive messages sent to exchanges. The queues are declared
by consumers.</p>
</blockquote>
</li>
<li><p class="first">Routing keys</p>
<blockquote>
<p>Every message has a routing key.  The interpretation of the routing
key depends on the exchange type. There are four default exchange
types defined by the AMQP standard, and vendors can define custom
types (so see your vendors manual for details).</p>
<p>These are the default exchange types defined by AMQP/0.8:</p>
<blockquote>
<ul>
<li><p class="first">Direct exchange</p>
<blockquote>
<p>Matches if the routing key property of the message and
the <tt class="docutils literal"><span class="pre">routing_key</span></tt> attribute of the consumer are identical.</p>
</blockquote>
</li>
<li><p class="first">Fan-out exchange</p>
<blockquote>
<p>Always matches, even if the binding does not have a routing
key.</p>
</blockquote>
</li>
<li><p class="first">Topic exchange</p>
<blockquote>
<p>Matches the routing key property of the message by a primitive
pattern matching scheme. The message routing key then consists
of words separated by dots (<tt class="docutils literal"><span class="pre">&quot;.&quot;</span></tt>, like domain names), and
two special characters are available; star (<tt class="docutils literal"><span class="pre">&quot;*&quot;</span></tt>) and hash
(<tt class="docutils literal"><span class="pre">&quot;#&quot;</span></tt>). The star matches any word, and the hash matches
zero or more words. For example <tt class="docutils literal"><span class="pre">&quot;*.stock.#&quot;</span></tt> matches the
routing keys <tt class="docutils literal"><span class="pre">&quot;usd.stock&quot;</span></tt> and <tt class="docutils literal"><span class="pre">&quot;eur.stock.db&quot;</span></tt> but not
<tt class="docutils literal"><span class="pre">&quot;stock.nasdaq&quot;</span></tt>.</p>
</blockquote>
</li>
</ul>
</blockquote>
</blockquote>
</li>
</ul>
</blockquote>
</div>
<div class="section" id="examples">
<h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3>
</div>
</div>
<div class="section" id="creating-a-connection">
<h2>Creating a connection<a class="headerlink" href="#creating-a-connection" title="Permalink to this headline">¶</a></h2>
<blockquote>
<p>You can set up a connection by creating an instance of
<tt class="docutils literal"><span class="pre">kombu.BrokerConnection</span></tt>, with the appropriate options for
your broker:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">kombu</span> <span class="kn">import</span> <span class="n">BrokerConnection</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">BrokerConnection</span><span class="p">(</span><span class="n">hostname</span><span class="o">=</span><span class="s">&quot;localhost&quot;</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">5672</span><span class="p">,</span>
<span class="gp">... </span>                        <span class="n">userid</span><span class="o">=</span><span class="s">&quot;guest&quot;</span><span class="p">,</span> <span class="n">password</span><span class="o">=</span><span class="s">&quot;guest&quot;</span><span class="p">,</span>
<span class="gp">... </span>                        <span class="n">virtual_host</span><span class="o">=</span><span class="s">&quot;/&quot;</span><span class="p">)</span>
</pre></div>
</div>
</blockquote>
</div>
<div class="section" id="receiving-messages-using-a-consumer">
<h2>Receiving messages using a Consumer<a class="headerlink" href="#receiving-messages-using-a-consumer" title="Permalink to this headline">¶</a></h2>
<p>First we open up a Python shell and start a message consumer.</p>
<p>This consumer declares a queue named <tt class="docutils literal"><span class="pre">&quot;feed&quot;</span></tt>, receiving messages with
the routing key <tt class="docutils literal"><span class="pre">&quot;importer&quot;</span></tt> from the <tt class="docutils literal"><span class="pre">&quot;feed&quot;</span></tt> exchange.</p>
<blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">kombu</span> <span class="kn">import</span> <span class="n">Exchange</span><span class="p">,</span> <span class="n">Queue</span><span class="p">,</span> <span class="n">Consumer</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">feed_exchange</span> <span class="o">=</span> <span class="n">Exchange</span><span class="p">(</span><span class="s">&quot;feed&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;direct&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">feed_queue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">(</span><span class="s">&quot;feed&quot;</span><span class="p">,</span> <span class="n">feed_exchange</span><span class="p">,</span> <span class="s">&quot;importer&quot;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">channel</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">channel</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">consumer</span> <span class="o">=</span> <span class="n">Consumer</span><span class="p">(</span><span class="n">channel</span><span class="p">,</span> <span class="p">[</span><span class="n">feed_queue</span><span class="p">])</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">import_feed_callback</span><span class="p">(</span><span class="n">message_data</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>
<span class="gp">... </span>    <span class="n">feed_url</span> <span class="o">=</span> <span class="n">message_data</span><span class="p">[</span><span class="s">&quot;import_feed&quot;</span><span class="p">]</span>
<span class="gp">... </span>    <span class="k">print</span><span class="p">(</span><span class="s">&quot;Got feed import message for: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">feed_url</span><span class="p">)</span>
<span class="gp">... </span>    <span class="c"># something importing this feed url</span>
<span class="gp">... </span>    <span class="c"># import_feed(feed_url)</span>
<span class="gp">... </span>    <span class="n">message</span><span class="o">.</span><span class="n">ack</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">consumer</span><span class="o">.</span><span class="n">register_callback</span><span class="p">(</span><span class="n">import_feed_callback</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Consume messages in a loop</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">connection</span><span class="o">.</span><span class="n">drain_events</span><span class="p">(</span><span class="n">timeout</span><span class="o">=...</span><span class="p">)</span>
</pre></div>
</div>
</blockquote>
</div>
<div class="section" id="sending-messages-using-a-producer">
<h2>Sending messages using a Producer<a class="headerlink" href="#sending-messages-using-a-producer" title="Permalink to this headline">¶</a></h2>
<p>Then we open up another Python shell to send some messages to the consumer
defined in the last section.</p>
<blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">kombu</span> <span class="kn">import</span> <span class="n">Exchange</span><span class="p">,</span> <span class="n">Producer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">feed_exchange</span> <span class="o">=</span> <span class="n">Exchange</span><span class="p">(</span><span class="s">&quot;feed&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;direct&quot;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">channel</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">channel</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">producer</span> <span class="o">=</span> <span class="n">Producer</span><span class="p">(</span><span class="n">channel</span><span class="p">,</span> <span class="n">feed_exchange</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">producer</span><span class="o">.</span><span class="n">publish</span><span class="p">({</span><span class="s">&quot;import_feed&quot;</span><span class="p">:</span> <span class="s">&quot;http://cnn.com/rss/edition.rss&quot;</span><span class="p">},</span>
<span class="gp">... </span>                 <span class="n">routing_key</span><span class="o">=</span><span class="s">&quot;importer&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">producer</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</blockquote>
<p>Look in the first Python shell again (where consumer loop is running),
where the following text has been printed to the screen:</p>
<div class="highlight-python"><pre>Got feed import message for: http://cnn.com/rss/edition.rss</pre>
</div>
</div>
<div class="section" id="serialization-of-data">
<h2>Serialization of Data<a class="headerlink" href="#serialization-of-data" title="Permalink to this headline">¶</a></h2>
<p>By default every message is encoded using <a class="reference external" href="http://www.json.org/">JSON</a>, so sending
Python data structures like dictionaries and lists works.
<a class="reference external" href="http://yaml.org/">YAML</a>, <a class="reference external" href="http://msgpack.sourceforge.net/">msgpack</a> and Python&#8217;s built-in <tt class="docutils literal"><span class="pre">pickle</span></tt> module is also supported,
and if needed you can register any custom serialization scheme you
want to use.</p>
<p>Each option has its advantages and disadvantages.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">json</span></tt> &#8211; JSON is supported in many programming languages, is now</dt>
<dd><p class="first">a standard part of Python (since 2.6), and is fairly fast to
decode using the modern Python libraries such as <tt class="docutils literal"><span class="pre">cjson</span> <span class="pre">or</span>
<span class="pre">``simplejson</span></tt>.</p>
<p>The primary disadvantage to <tt class="docutils literal"><span class="pre">JSON</span></tt> is that it limits you to
the following data types: strings, unicode, floats, boolean,
dictionaries, and lists.  Decimals and dates are notably missing.</p>
<p>Also, binary data will be transferred using base64 encoding, which
will cause the transferred data to be around 34% larger than an
encoding which supports native binary types.</p>
<p class="last">However, if your data fits inside the above constraints and
you need cross-language support, the default setting of <tt class="docutils literal"><span class="pre">JSON</span></tt>
is probably your best choice.</p>
</dd>
<dt><tt class="docutils literal"><span class="pre">pickle</span></tt> &#8211; If you have no desire to support any language other than</dt>
<dd>Python, then using the <tt class="docutils literal"><span class="pre">pickle</span></tt> encoding will gain you
the support of all built-in Python data types (except class instances),
smaller messages when sending binary files, and a slight speedup
over <tt class="docutils literal"><span class="pre">JSON</span></tt> processing.</dd>
<dt><tt class="docutils literal"><span class="pre">yaml</span></tt> &#8211; YAML has many of the same characteristics as <tt class="docutils literal"><span class="pre">json</span></tt>,</dt>
<dd><p class="first">except that it natively supports more data types (including dates,
recursive references, etc.)</p>
<p>However, the Python libraries for YAML are a good bit slower
than the libraries for JSON.</p>
<p class="last">If you need a more expressive set of data types and need to maintain
cross-language compatibility, then <tt class="docutils literal"><span class="pre">YAML</span></tt> may be a better fit
than the above.</p>
</dd>
</dl>
<p>To instruct carrot to use an alternate serialization method,
use one of the following options.</p>
<blockquote>
<ol class="arabic">
<li><p class="first">Set the serialization option on a per-producer basis:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">producer</span> <span class="o">=</span> <span class="n">Producer</span><span class="p">(</span><span class="n">channel</span><span class="p">,</span>
<span class="gp">... </span>                    <span class="n">exchange</span><span class="o">=</span><span class="n">exchange</span><span class="p">,</span>
<span class="gp">... </span>                    <span class="n">serializer</span><span class="o">=</span><span class="s">&quot;yaml&quot;</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">Set the serialization option per message:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">producer</span><span class="o">.</span><span class="n">publish</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">routing_key</span><span class="o">=</span><span class="n">rkey</span><span class="p">,</span>
<span class="gp">... </span>                 <span class="n">serializer</span><span class="o">=</span><span class="s">&quot;pickle&quot;</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ol>
</blockquote>
<p>Note that a <tt class="docutils literal"><span class="pre">Consumer</span></tt> do not need the serialization method specified.
They can auto-detect the serialization method as the
content-type is sent as a message header.</p>
</div>
<div class="section" id="sending-raw-data-without-serialization">
<h2>Sending raw data without Serialization<a class="headerlink" href="#sending-raw-data-without-serialization" title="Permalink to this headline">¶</a></h2>
<p>In some cases, you don&#8217;t need your message data to be serialized. If you
pass in a plain string or unicode object as your message, then carrot will
not waste cycles serializing/deserializing the data.</p>
<p>You can optionally specify a <tt class="docutils literal"><span class="pre">content_type</span></tt> and <tt class="docutils literal"><span class="pre">content_encoding</span></tt>
for the raw data:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">producer</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">&#39;~/my_picture.jpg&#39;</span><span class="p">,</span><span class="s">&#39;rb&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">(),</span>
<span class="go">                  content_type=&quot;image/jpeg&quot;,</span>
<span class="go">                  content_encoding=&quot;binary&quot;,</span>
<span class="go">                  routing_key=rkey)</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">Message</span></tt> object returned by the <tt class="docutils literal"><span class="pre">Consumer</span></tt> class will have a
<tt class="docutils literal"><span class="pre">content_type</span></tt> and <tt class="docutils literal"><span class="pre">content_encoding</span></tt> attribute.</p>
</div>
<div class="section" id="receiving-messages-without-a-callback">
<h2>Receiving messages without a callback<a class="headerlink" href="#receiving-messages-without-a-callback" title="Permalink to this headline">¶</a></h2>
<p>You can also poll the queue manually, by using the <tt class="docutils literal"><span class="pre">get</span></tt> method.
This method returns a <tt class="docutils literal"><span class="pre">Message</span></tt> object, from where you can get the
message body, de-serialize the body to get the data, acknowledge, reject or
re-queue the message.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">consumer</span> <span class="o">=</span> <span class="n">Consumer</span><span class="p">(</span><span class="n">channel</span><span class="p">,</span> <span class="n">queues</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">message</span> <span class="o">=</span> <span class="n">consumer</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">if</span> <span class="n">message</span><span class="p">:</span>
<span class="gp">... </span>   <span class="n">message_data</span> <span class="o">=</span> <span class="n">message</span><span class="o">.</span><span class="n">payload</span>
<span class="gp">... </span>   <span class="n">message</span><span class="o">.</span><span class="n">ack</span><span class="p">()</span>
<span class="gp">... </span><span class="k">else</span><span class="p">:</span>
<span class="gp">... </span>    <span class="c"># No messages waiting on the queue.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">consumer</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="sub-classing-the-messaging-classes">
<h2>Sub-classing the messaging classes<a class="headerlink" href="#sub-classing-the-messaging-classes" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">Consumer</span></tt>, and <tt class="docutils literal"><span class="pre">Producer</span></tt> classes can also be sub classed. Thus you
can define the above producer and consumer like so:</p>
<blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">FeedProducer</span><span class="p">(</span><span class="n">Producer</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">exchange</span> <span class="o">=</span> <span class="n">exchange</span>
<span class="gp">... </span>    <span class="n">routing_key</span> <span class="o">=</span> <span class="s">&quot;importer&quot;</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">import_feed</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">feed_url</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">publish</span><span class="p">({</span><span class="s">&quot;action&quot;</span><span class="p">:</span> <span class="s">&quot;import_feed&quot;</span><span class="p">,</span>
<span class="gp">... </span>                             <span class="s">&quot;feed_url&quot;</span><span class="p">:</span> <span class="n">feed_url</span><span class="p">})</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">FeedConsumer</span><span class="p">(</span><span class="n">Consumer</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">queues</span> <span class="o">=</span> <span class="n">queues</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">receive</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message_data</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
<span class="gp">... </span>        <span class="n">action</span> <span class="o">=</span> <span class="n">message_data</span><span class="p">[</span><span class="s">&quot;action&quot;</span><span class="p">]</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="n">action</span> <span class="o">==</span> <span class="s">&quot;import_feed&quot;</span><span class="p">:</span>
<span class="gp">... </span>            <span class="c"># something importing this feed</span>
<span class="gp">... </span>            <span class="c"># import_feed(message_data[&quot;feed_url&quot;])</span>
<span class="go">                message.ack()</span>
<span class="gp">... </span>        <span class="k">else</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&quot;Unknown action: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">action</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">producer</span> <span class="o">=</span> <span class="n">FeedProducer</span><span class="p">(</span><span class="n">channel</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">producer</span><span class="o">.</span><span class="n">import_feed</span><span class="p">(</span><span class="s">&quot;http://cnn.com/rss/edition.rss&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">producer</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">consumer</span> <span class="o">=</span> <span class="n">FeedConsumer</span><span class="p">(</span><span class="n">channel</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">connection</span><span class="o">.</span><span class="n">drain_events</span><span class="p">()</span>
</pre></div>
</div>
</blockquote>
<div class="section" id="getting-help">
<h3>Getting Help<a class="headerlink" href="#getting-help" title="Permalink to this headline">¶</a></h3>
</div>
</div>
<div class="section" id="mailing-list">
<h2>Mailing list<a class="headerlink" href="#mailing-list" title="Permalink to this headline">¶</a></h2>
<p>Join the <a class="reference external" href="http://groups.google.com/group/carrot-users/">carrot-users</a> mailing list.</p>
<div class="section" id="bug-tracker">
<h3>Bug tracker<a class="headerlink" href="#bug-tracker" title="Permalink to this headline">¶</a></h3>
<p>If you have any suggestions, bug reports or annoyances please report them
to our issue tracker at <a class="reference external" href="http://github.com/ask/kombu/issues/">http://github.com/ask/kombu/issues/</a></p>
</div>
<div class="section" id="contributing">
<h3>Contributing<a class="headerlink" href="#contributing" title="Permalink to this headline">¶</a></h3>
<p>Development of <tt class="docutils literal"><span class="pre">kombu</span></tt> happens at Github: <a class="reference external" href="http://github.com/ask/kombu">http://github.com/ask/kombu</a></p>
<p>You are highly encouraged to participate in the development. If you don&#8217;t
like Github (for some reason) you&#8217;re welcome to send regular patches.</p>
</div>
<div class="section" id="license">
<h3>License<a class="headerlink" href="#license" title="Permalink to this headline">¶</a></h3>
<p>This software is licensed under the <tt class="docutils literal"><span class="pre">New</span> <span class="pre">BSD</span> <span class="pre">License</span></tt>. See the <tt class="docutils literal"><span class="pre">LICENSE</span></tt>
file in the top distribution directory for the full license text.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper"><p class="logo"><a href="index.html">
  <img class="logo" width="128" height="128" src="http://cloud.github.com/downloads/ask/kombu/kombusmall.jpg" alt="Logo"/>
</a></p>
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">kombu - AMQP Messaging Framework for Python</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#documentation">Documentation</a><ul>
<li><a class="reference internal" href="#installation">Installation</a></li>
<li><a class="reference internal" href="#terminology">Terminology</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
<li><a class="reference internal" href="#creating-a-connection">Creating a connection</a></li>
<li><a class="reference internal" href="#receiving-messages-using-a-consumer">Receiving messages using a Consumer</a></li>
<li><a class="reference internal" href="#sending-messages-using-a-producer">Sending messages using a Producer</a></li>
<li><a class="reference internal" href="#serialization-of-data">Serialization of Data</a></li>
<li><a class="reference internal" href="#sending-raw-data-without-serialization">Sending raw data without Serialization</a></li>
<li><a class="reference internal" href="#receiving-messages-without-a-callback">Receiving messages without a callback</a></li>
<li><a class="reference internal" href="#sub-classing-the-messaging-classes">Sub-classing the messaging classes</a><ul>
<li><a class="reference internal" href="#getting-help">Getting Help</a></li>
</ul>
</li>
<li><a class="reference internal" href="#mailing-list">Mailing list</a><ul>
<li><a class="reference internal" href="#bug-tracker">Bug tracker</a></li>
<li><a class="reference internal" href="#contributing">Contributing</a></li>
<li><a class="reference internal" href="#license">License</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">Kombu Documentation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="faq.html"
                        title="next chapter">Frequently Asked Questions</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="sources/introduction.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="faq.html" title="Frequently Asked Questions"
             >next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Kombu Documentation"
             >previous</a> |</li>
        <li><a href="index.html">Kombu v0.1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2009-2010, Ask Solem.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0b2.
    </div>
  </body>
</html>