@@ -131,12 +131,12 @@ Our overall design will look like:
131
131
>
132
132
> * On Ubuntu:
133
133
>
134
- > $ sudo apt-get install python-pip
134
+ > $ sudo apt-get install python-pip
135
135
>
136
136
> * On Debian:
137
137
>
138
- > $ sudo apt-get install python-setuptools
139
- > $ sudo easy_install pip
138
+ > $ sudo apt-get install python-setuptools
139
+ > $ sudo easy_install pip
140
140
141
141
### Sending
142
142
@@ -150,31 +150,31 @@ Our overall design will look like:
150
150
Our first program ` send.py ` will send a single message to the queue.
151
151
The first thing we need to do is connect to RabbitMQ server.
152
152
153
- <table class =" highlighttable " ><tr ><td class =" linenos " ><div class =" linenodiv " ><pre ><code class =" python " >1
154
- 2
155
- 3
156
- 4
157
- 5
158
- 6
159
- 7</code ></pre ></div ></td ><td class =" code " ><div class =" highlight " ><pre ><span class =" c " >#!/usr/bin/env python</span >
160
- <span class =" kn " >import</span > <span class =" nn " >pika</span >
153
+ <div >
154
+ <pre >
155
+ <code class =' python ' >#!/usr/bin/env python
156
+ import pika
157
+
158
+ connection = pika.AsyncoreConnection(pika.ConnectionParameters(
159
+ '127.0.0.1',
160
+ credentials = pika.PlainCredentials('guest', 'guest'))
161
+ channel = connection.channel()</code >
162
+ </pre >
163
+ </div >
161
164
162
- <span class =" n " >connection</span > <span class =" o " >=</span > <span class =" n " >pika</span ><span class =" o " >.</span ><span class =" n " >AsyncoreConnection</span ><span class =" p " >(</span ><span class =" n " >pika</span ><span class =" o " >.</span ><span class =" n " >ConnectionParameters</span ><span class =" p " >(</span >
163
- <span class =" s " >' ; 127.0.0.1' ; </span ><span class =" p " >,</span >
164
- <span class =" n " >credentials</span > <span class =" o " >=</span > <span class =" n " >pika</span ><span class =" o " >.</span ><span class =" n " >PlainCredentials</span ><span class =" p " >(</span ><span class =" s " >' ; guest' ; </span ><span class =" p " >,</span > <span class =" s " >' ; guest' ; </span ><span class =" p " >))</span >
165
- <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 >
166
- </pre ></div >
167
- </td ></tr ></table >
168
165
169
166
170
167
Whenever we send a message we need to make sure the recipient queue exists.
171
168
RabbitMQ will just trash the message if can't deliver it. So, we need to
172
169
create a queue to which the message will be delivered. Let's name this queue
173
170
_ test_ :
174
171
175
- <table class =" highlighttable " ><tr ><td class =" linenos " ><div class =" linenodiv " ><pre ><code class =" python " >8</code ></pre ></div ></td ><td class =" code " ><div class =" highlight " ><pre ><span class =" n " >channel</span ><span class =" o " >.</span ><span class =" n " >queue_declare</span ><span class =" p " >(</span ><span class =" n " >queue</span ><span class =" o " >=</span ><span class =" s " >' ; test' ; </span ><span class =" p " >)</span >
176
- </pre ></div >
177
- </td ></tr ></table >
172
+ <div >
173
+ <pre >
174
+ <code class =' python ' >channel.queue_declare(queue='test')</code >
175
+ </pre >
176
+ </div >
177
+
178
178
179
179
180
180
At that point we're ready to send a message. Our first message will
@@ -189,15 +189,15 @@ identified by an empty string. That exchange is a special one that
189
189
allows us to specify exactly to which queue the message should go.
190
190
The queue name is specified by the ` routing_key ` variable:
191
191
192
- <table class = " highlighttable " >< tr >< td class = " linenos " >< div class = " linenodiv " >< pre >< code class = " python " > 9
193
- 10
194
- 11
195
- 12</ code ></ pre ></ div ></ td >< td class = " code " >< div class = " highlight " >< pre >< span class = " n " >channel</ span >< span class = " o " >.</ span >< span class = " n " >basic_publish</ span >< span class = " p " >(</ span >< span class = " n " >exchange</ span >< span class = " o " >=</ span >< span class = " s " > &# 39 ;&# 39 ; </ span >< span class = " p " >,</ span >
196
- < span class = " n " >routing_key</ span >< span class = " o " >=</ span >< span class = " s " > &# 39 ; test &# 39 ; </ span >< span class = " p " >,</ span >
197
- < span class = " n " >body</ span >< span class = " o " >=</ span >< span class = " s " > &# 39 ; Hello World!&# 39 ; </span >< span class = " p " >)</ span >
198
- < span class = " k " >print</ span > < span class = " s " > & quot ; [ x ] Sent &# 39 ; Hello World! &# 39 ; & quot ; </ span >
199
- </pre ></ div >
200
- </ td ></ tr ></ table >
192
+ <div >
193
+ < pre >
194
+ < code class = ' python ' >channel.basic_publish(exchange='',
195
+ routing_key='test',
196
+ body='Hello World!')
197
+ print & quot ; [x] Sent ' Hello World!' & quot ; </code >
198
+ </ pre >
199
+ </div >
200
+
201
201
202
202
203
203
[ (full send.py source)] ( http://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/send.py )
@@ -218,15 +218,18 @@ them on the screen.
218
218
The code responsible for connecting to Rabbit is the same as the previous example.
219
219
You can copy the first 7 lines.
220
220
221
- # ... connection code is the same, copy first 7 lines from send.py ...
221
+ # ... connection code is the same, copy first 7 lines from send.py ...
222
222
223
223
Just like before, in the beginning we must make sure that the
224
224
queue exists. Creating a queue using ` queue_declare ` is idempotent - you can
225
225
run the command as many times you like, and only one queue will be created.
226
226
227
- <table class =" highlighttable " ><tr ><td class =" linenos " ><div class =" linenodiv " ><pre ><code class =" python " >8</code ></pre ></div ></td ><td class =" code " ><div class =" highlight " ><pre ><span class =" n " >channel</span ><span class =" o " >.</span ><span class =" n " >queue_declare</span ><span class =" p " >(</span ><span class =" n " >queue</span ><span class =" o " >=</span ><span class =" s " >' ; test' ; </span ><span class =" p " >)</span >
228
- </pre ></div >
229
- </td ></tr ></table >
227
+ <div >
228
+ <pre >
229
+ <code class =' python ' >channel.queue_declare(queue='test')</code >
230
+ </pre >
231
+ </div >
232
+
230
233
231
234
You may ask why to declare queue again - we have already declared it
232
235
in our previous code. We could have avoided that if we always run the
@@ -239,23 +242,26 @@ Receiving messages from the queue is a bit more complex. Whenever we receive
239
242
a message, a ` callback ` function is called. In our case
240
243
this function will print on the screen the contents of the message.
241
244
242
- <table class =" highlighttable " ><tr ><td class =" linenos " ><div class =" linenodiv " ><pre ><code class =" python " > 9
243
- 10</code ></pre ></div ></td ><td class =" code " ><div class =" highlight " ><pre ><span class =" k " >def</span > <span class =" nf " >callback</span ><span class =" p " >(</span ><span class =" n " >ch</span ><span class =" p " >,</span > <span class =" n " >method</span ><span class =" p " >,</span > <span class =" n " >header</span ><span class =" p " >,</span > <span class =" n " >body</span ><span class =" p " >):</span >
244
- <span class =" k " >print</span > <span class =" s " >" ; [ x] Received </span ><span class =" si " >%.20r</span ><span class =" s " >" ; </span > <span class =" o " >%</span > <span class =" p " >(</span ><span class =" n " >body</span ><span class =" p " >,)</span >
245
- </pre ></div >
246
- </td ></tr ></table >
245
+ <div >
246
+ <pre >
247
+ <code class =' python ' >def callback(ch, method, header, body):
248
+ print " ; [x] Received %.20r" ; % (body,)</code >
249
+ </pre >
250
+ </div >
251
+
247
252
248
253
249
254
Next, we need to tell RabbitMQ that this particular callback function is
250
255
interested in messages from our _ test_ queue:
251
256
252
- <table class =" highlighttable " ><tr ><td class =" linenos " ><div class =" linenodiv " ><pre ><code class =" python " >11
253
- 12
254
- 13</code ></pre ></div ></td ><td class =" code " ><div class =" highlight " ><pre ><span class =" n " >channel</span ><span class =" o " >.</span ><span class =" n " >basic_consume</span ><span class =" p " >(</span ><span class =" n " >callback</span ><span class =" p " >,</span >
255
- <span class =" n " >queue</span ><span class =" o " >=</span ><span class =" s " >' ; test' ; </span ><span class =" p " >,</span >
256
- <span class =" n " >no_ack</span ><span class =" o " >=</span ><span class =" bp " >True</span ><span class =" p " >)</span >
257
- </pre ></div >
258
- </td ></tr ></table >
257
+ <div >
258
+ <pre >
259
+ <code class =' python ' >channel.basic_consume(callback,
260
+ queue='test',
261
+ no_ack=True)</code >
262
+ </pre >
263
+ </div >
264
+
259
265
260
266
For that command to succeed we must be sure that a queue which we want
261
267
to subscribe to exists. Fortunately we're confident about that - we've
@@ -264,11 +270,13 @@ created a queue above - using `queue_declare`.
264
270
And finally, we enter a never-ending loop that waits for data and runs callbacks
265
271
whenever necessary.
266
272
267
- <table class =" highlighttable " ><tr ><td class =" linenos " ><div class =" linenodiv " ><pre ><code class =" python " >14
268
- 15</code ></pre ></div ></td ><td class =" code " ><div class =" highlight " ><pre ><span class =" k " >print</span > <span class =" s " >' ; [ * ] Waiting for messages. To exit press CTRL+C' ; </span >
269
- <span class =" n " >pika</span ><span class =" o " >.</span ><span class =" n " >asyncore_loop</span ><span class =" p " >()</span >
270
- </pre ></div >
271
- </td ></tr ></table >
273
+ <div >
274
+ <pre >
275
+ <code class =' python ' >print ' [*] Waiting for messages. To exit press CTRL+C'
276
+ pika.asyncore_loop()</code >
277
+ </pre >
278
+ </div >
279
+
272
280
273
281
[ (full receive.py source)] ( http://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/receive.py )
274
282
0 commit comments