8000 python/1 fixes · ikulis/rabbitmq-tutorials@3150621 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3150621

Browse files
committed
python/1 fixes
1 parent b92e41c commit 3150621

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed

python/tutorial-one.mdx

Lines changed: 67 additions & 32 deletions
Original file line number
Diff line numberDiff line change
@@ -84,10 +84,13 @@ RabbitMQ uses a weird jargon, but it's simple once you'll get it. For example:
8484
messages that go to the one queue, many _consumers_ can try to
8585
receive data from one _queue_.
8686

87-
{% dot -Gsize="10,0.3" -Grankdir=LR%}
88-
87+
{% dot -Gsize="10,0.9" -Grankdir=LR%}
8988
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+
};
9194
}
9295
{% enddot %}
9396

@@ -114,7 +117,7 @@ Our overall design will look like:
114117
digraph G {
115118
P1 [label="P", {{ dotstyle.producer }}];
116119
subgraph cluster_Q1 {
117-
label="queue_name=test";
120+
label="test";
118121
color=transparent;
119122
Q1 [label="{<s>||||<e>}", {{ dotstyle.queue }}];
120123
};
@@ -130,16 +133,17 @@ digraph G {
130133
> understands the same protocol as Rabbit. There is a choice of libraries
131134
> for almost every programming language. Python it's not different and there is
132135
> a bunch of libraries to choose from:
136+
>
133137
> * [py-amqplib](http://barryp.org/software/py-amqplib/)
134138
> * [txAMQP](https://launchpad.net/txamqp)
135139
> * [pika](http://github.com/tonyg/pika)
136140
>
137141
> In this tutorial we're going to use `pika`. To install it you can use
138142
> [`pip`](http://pip.openplans.org/) package management tool:
139143
>
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
141145
>
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.
143147
>
144148
> * On Ubuntu:
145149
>
@@ -156,7 +160,7 @@ digraph G {
156160
digraph G {
157161
P1 [label="P", {{ dotstyle.producer }}];
158162
subgraph cluster_Q1 {
159-
label="queue_name=test";
163+
label="test";
160164
color=transparent;
161165
Q1 [label="{<s>||||<e>}", {{ dotstyle.queue }}];
162166
};
@@ -169,17 +173,14 @@ digraph G {
169173
Our first program `send.py` will send a single message to the queue.
170174
The first thing we need to do is connect to RabbitMQ server.
171175

172-
{% highlight python linenos=true linenostart=1 %}
173-
174-
176+
{% highlight python %}
175177
#!/usr/bin/env python
176178
import pika
177179

178180
connection = pika.AsyncoreConnection(pika.ConnectionParameters(
179181
'127.0.0.1',
180182
credentials = pika.PlainCredentials('guest', 'guest'))
181183
channel = connection.channel()
182-
183184
{% endhighlight %}
184185

185186

@@ -188,10 +189,8 @@ RabbitMQ will just trash the message if can't deliver it. So, we need to
188189
create a queue to which the message will be delivered. Let's name this queue
189190
_test_:
190191

191-
{% highlight python linenos=true linenostart=8 %}
192-
192+
{% highlight python %}
193193
channel.queue_declare(queue='test')
194-
195194
{% endhighlight %}
196195

197196

@@ -207,18 +206,14 @@ identified by an empty string. That exchange is a special one that
207206
allows us to specify exactly to which queue the message should go.
208207
The queue name is specified by the `routing_key` variable:
209208

210-
{% highlight python linenos=true linenostart=9 %}
211-
209+
{% highlight python %}
212210
channel.basic_publish(exchange='',
213211
routing_key='test',
214212
body='Hello World!')
215213
print " [x] Sent 'Hello World!'"
216-
217214
{% endhighlight %}
218215

219216

220-
[(full send.py source)]({{ examples_url }}/python/send.py)
221-
222217
### Receiving
223218

224219

@@ -227,7 +222,7 @@ digraph G {
227222
rankdir=LR;
228223

229224
subgraph cluster_Q1 {
230-
label="queue_name=test";
225+
label="test";
231226
color=transparent;
232227
Q1 [label="{<s>||||<e>}", {{ dotstyle.queue }}];
233228
};
@@ -250,10 +245,8 @@ Just like before, in the beginning we must make sure that the
250245
queue exists. Creating a queue using `queue_declare` is idempotent - you can
251246
run the command as many times you like, and only one queue will be created.
252247

253-
{% highlight python linenos=true linenostart=8 %}
254-
248+
{% highlight python %}
255249
channel.queue_declare(queue='test')
256-
257250
{% endhighlight %}
258251

259252
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
278271
a message, a `callback` function is called. In our case
279272
this function will print on the screen the contents of the message.
280273

281-
{% highlight python linenos=true linenostart=9 %}
282-
274+
{% highlight python %}
283275
def callback(ch, method, header, body):
284276
print " [x] Received %.20r" % (body,)
285-
286277
{% endhighlight %}
287278

288279

289280
Next, we need to tell RabbitMQ that this particular callback function is
290281
interested in messages from our _test_ queue:
291282

292-
{% highlight python linenos=true linenostart=11 %}
293-
283+
{% highlight python %}
294284
channel.basic_consume(callback,
295285
queue='test',
296286
no_ack=True)
297-
298287
{% endhighlight %}
299288

300289
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`.
304293
And finally, we enter a never-ending loop that waits for data and runs callbacks
305294
whenever necessary.
306295

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+
308336

337+
channel.queue_declare(queue='test')
309338

310339
print ' [*] Waiting for messages. To exit press CTRL+C'
311-
pika.asyncore_loop()
312340

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()
313349
{% endhighlight %}
314350

315351
[(full receive.py source)]({{ examples_url }}/python/receive.py)
316352

317-
### Putting it all together
318353

319354
Now we can try out our programs. First, let's send a message using our
320355
`send.py` program:

0 commit comments

Comments
 (0)
0