@@ -84,10 +84,13 @@ RabbitMQ uses a weird jargon, but it's simple once you'll get it. For example:
84
84
messages that go to the one queue , many _consumers_ can try to
85
85
receive data from one _queue_.
86
86
87
- {% dot -Gsize = " 10,0.3" - Grankdir = LR % }
88
-
87
+ {% dot -Gsize = " 10,0.9" - Grankdir = LR % }
89
88
digraph G {
90
- Q1 [label = " {<s>||||<e>}" , {{ dotstyle .queue }}];
89
+ subgraph cluster_Q1 {
90
+ label = " queue_name" ;
91
+ color = transparent ;
92
+ Q1 [label = " {<s>||||<e>}" , {{ dotstyle .queue }}];
93
+ };
91
94
}
92
95
{% enddot %}
93
96
@@ -114,7 +117,7 @@ Our overall design will look like:
114
117
digraph G {
115
118
P1 [label = " P" , {{ dotstyle .producer }}];
116
119
subgraph cluster_Q1 {
117
- label= " queue_name= test" ;
120
+ label= " test" ;
118
121
color= transparent ;
119
122
Q1 [label = " {<s>||||<e>}" , {{ dotstyle .queue }}];
120
123
};
@@ -130,16 +133,17 @@ digraph G {
130
133
> understands the same protocol as Rabbit . There is a choice of libraries
131
134
> for almost every programming language . Python it ' s not different and there is
132
135
> a bunch of libraries to choose from :
136
+ >
133
137
> * [py -amqplib ](http : // barryp.org/software/py-amqplib/)
134
138
> * [txAMQP ](https : // launchpad.net/txamqp)
135
139
> * [pika ](http : // github.com/tonyg/pika)
136
140
>
137
141
> In this tutorial we ' re going to use `pika`. To install it you can use
138
142
> [` pip ` ](http : // pip.openplans.org/) package management tool:
139
143
>
140
- > $ sudo pip install -e git +http : // github.com/tonyg/pika.git#egg=pika
144
+ > $ sudo pip install -e git +http : // github.com/tonyg/pika.git#egg=pika
141
145
>
142
- >If you don ' t have `pip`, you may want to install it.
146
+ > If you don ' t have `pip`, you may need to install it.
143
147
>
144
148
> * On Ubuntu :
145
149
>
@@ -156,7 +160,7 @@ digraph G {
156
160
digraph G {
157
161
P1 [label = " P" , {{ dotstyle .producer }}];
158
162
subgraph cluster_Q1 {
159
- label= " queue_name= test" ;
163
+ label= " test" ;
160
164
color= transparent ;
161
165
Q1 [label = " {<s>||||<e>}" , {{ dotstyle .queue }}];
162
166
};
@@ -169,17 +173,14 @@ digraph G {
169
173
Our first program ` send.py ` will send a single message to the queue .
170
174
The first thing we need to do is connect to RabbitMQ server .
171
175
172
- {% highlight python linenos= true linenostart = 1 % }
173
-
174
-
176
+ {% highlight python %}
175
177
#!/usr /bin /env python
176
178
import pika
177
179
178
180
connection = pika .AsyncoreConnection (pika .ConnectionParameters (
179
181
' 127.0.0.1' ,
180
182
credentials = pika .PlainCredentials (' guest' , ' guest' ))
181
183
channel = connection .channel ()
182
-
183
184
{% endhighlight %}
184
185
185
186
@@ -188,10 +189,8 @@ RabbitMQ will just trash the message if can't deliver it. So, we need to
188
189
create a queue to which the message will be delivered . Let ' s name this queue
189
190
_test_ :
190
191
191
- {% highlight python linenos = true linenostart = 8 % }
192
-
192
+ {% highlight python %}
193
193
channel .queue_declare (queue = ' test' )
194
-
195
194
{% endhighlight %}
196
195
197
196
@@ -207,18 +206,14 @@ identified by an empty string. That exchange is a special one that
207
206
allows us to specify exactly to which queue the message should go .
208
207
The queue name is specified by the ` routing_key ` variable :
209
208
210
- {% highlight python linenos = true linenostart = 9 % }
211
-
209
+ {% highlight python %}
212
210
channel .basic_publish (exchange = ' ' ,
213
211
routing_key = ' test' ,
214
212
body = ' Hello World!' )
215
213
print " [x] Sent 'Hello World!'"
216
-
217
214
{% endhighlight %}
218
215
219
216
220
- [(full send .py source )]({{ examples_url }}/ python / send .py )
221
-
222
217
### Receiving
223
218
224
219
@@ -227,7 +222,7 @@ digraph G {
227
222
rankdir = LR ;
228
223
229
224
subgraph cluster_Q1 {
230
- label = " queue_name= test" ;
225
+ label = " test" ;
231
226
color = transparent ;
232
227
Q1 [label = " {<s>||||<e>}" , {{ dotstyle .queue }}];
233
228
};
@@ -250,10 +245,8 @@ Just like before, in the beginning we must make sure that the
250
245
queue exists. Creating a queue using `queue_declare` is idempotent - you can
251
246
run the command as many times you like, and only one queue will be created.
252
247
253
- {% highlight python linenos=true linenostart=8 %}
254
-
248
+ {% highlight python %}
255
249
channel.queue_declare(queue='test')
256
-
257
250
{% endhighlight %}
258
251
259
252
You may ask why to declare queue again - we have already declared it
@@ -278,23 +271,19 @@ Receiving messages from the queue is a bit more complex. Whenever we receive
278
271
a message , a ` callback ` function is called. In our case
279
272
this function will print on the screen the contents of the message.
280
273
281
- {% highlight python linenos=true linenostart=9 %}
282
-
274
+ {% highlight python %}
283
275
def callback(ch, method, header, body):
284
276
print " [x] Received %.20r" % (body ,)
285
-
286
277
{% endhighlight %}
287
278
288
279
289
280
Next , we need to tell RabbitMQ that this particular callback function is
290
281
interested in messages from our _test_ queue:
291
282
292
- {% highlight python linenos = true linenostart = 11 % }
293
-
283
+ {% highlight python %}
294
284
channel .basic_consume (callback ,
295
285
queue = ' test' ,
296
286
no_ack = True )
297
-
298
287
{% endhighlight %}
299
288
300
289
For that command to succeed we must be sure that a queue which we want
@@ -304,17 +293,63 @@ created a queue above - using `queue_declare`.
304
293
And finally , we enter a never-ending loop that waits for data and runs callbacks
305
294
whenever necessary.
306
295
307
- {% highlight python linenos = true linenostart = 14 % }
296
+ {% highlight python % }
297
+ print ' [*] Waiting for messages. To exit press CTRL+C'
298
+ pika.asyncore_loop()
299
+ {% endhighlight %}
300
+
301
+
302
+ ### Putting it all together
303
+
304
+
305
+ Full code for ` send.py ` :
306
+ {% highlight python linenos = true % }
307
+ #! / usr / bin / env python
308
+ import pika
309
+
310
+ connection = pika .AsyncoreConnection (pika .ConnectionParameters (
311
+ host = ' 127.0.0.1' ,
312
+ credentials = pika .PlainCredentials (' guest' , ' guest' )))
313
+ channel = connection .channel ()
314
+
315
+
316
+ channel .queue_declare (queue = ' test' )
317
+
318
+ channel .basic_publish (exchange = ' ' ,
319
+ routing_key = ' test' ,
320
+ body = ' Hello World!' )
321
+ print " [x] Sent 'Hello World!'"
322
+ {% endhighlight %}
323
+ [(full send .py source )]({{ examples_url }}/ python / send .py )
324
+
325
+
326
+ Full ` receive.py ` code :
327
+ {% highlight python linenos = true % }
328
+ #! / usr / bin / env python
329
+ import pika
330
+
331
+ connection = pika .AsyncoreConnection (pika .ConnectionParameters (
332
+ host = ' 127.0.0.1' ,
333
+ credentials = pika .PlainCredentials (' guest' , ' guest' )))
334
+ channel = connection .channel ()
335
+
308
336
337
+ channel .queue_declare (queue = ' test' )
309
338
310
339
print ' [*] Waiting for messages. To exit press CTRL+C'
311
- pika.asyncore_loop()
312
340
341
+ def callback (ch , method , header , body ):
342
+ print " [x] Received %.20r" % (body ,)
343
+
344
+ channel .basic_consume (callback ,
345
+ queue = ' test' ,
346
+ no_ack = True )
347
+
348
+ pika .asyncore_loop ()
313
349
{% endhighlight %}
314
350
315
351
[(full receive .py source )]({{ examples_url }}/ python / receive .py )
316
352
317
- ### Putting it all together
318
353
319
354
Now we can try out our programs . First , let' s send a message using our
320
355
` send.py ` program:
0 commit comments