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

Skip to content

Commit 2fa5d97

Browse files
committed
python/1 fixes
1 parent 3150621 commit 2fa5d97

File tree

2 files changed

+61
-45
lines changed

2 files changed

+61
-45
lines changed

github.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ dot:
1313
pygments: false
1414

1515
examples_url: http://github.com/rabbitmq/rabbitmq-tutorials/blob/master
16+
17+
18+
python_one_url: http://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/tutorial-one.md
19+
20+
python_two_url: http://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/tutorial-two.md
21+
22+
python_three_url: http://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/tutorial-three.md

python/tutorial-one.mdx

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ digraph G {
1717

1818
Throughout this tutorial, we'll teach you the basic concepts required for
1919
creating RabbitMQ applications. The tutorial will be illustrated with
20-
code snippets written in [Python](http://www.python.org/). But don't worry if
21-
you don't know this language - the core ideas are the same for other languages.
20+
code snippets written in [Python](http://www.python.org/) and executed on Linux.
21+
But don't worry if you don't know this language - the core ideas are the same
22+
for other languages.
23+
2224

2325
This tutorial consists of three parts:
2426

2527
* First we're going to write the simplest possible "Hello World" example.
26-
* Next we'll try to use Rabbit as a simple "Work queue" server.
27-
* Finally, we'll discuss the "Publish-subscribe" pattern.
28+
* Next we'll try to use Rabbit as [a simple "Work queue" server]({{ python_two_url }}).
29+
* Finally, we'll discuss how to [broadcast a message]({{ python_three_url }}).
2830

2931
You need to have RabbitMQ server installed to go through this tutorial.
3032
If you haven't installed it yet you can follow the
@@ -49,15 +51,16 @@ If you have installed RabbitMQ you should see something like:
4951
> #### Where to get help
5052
>
5153
> If you're having trouble going through this tutorial you can post a message to
52-
> [rabbitmq-discuss mailing list](https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss).
54+
> [rabbitmq-discuss mailing list](https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss)
55+
> or join the [#rabbitmq](irc://irc.freenode.net/rabbitmq) irc channel.
5356

5457

5558
Introduction
5659
------------
5760

5861
RabbitMQ is a message broker. The principle idea is pretty simple: it accepts
5962
and forwards messages. You can think about it as a post office: when you E7F5 send
60-
mail to the post box and you're pretty sure that mr postman will eventually
63+
mail to the post box and you're pretty sure that Mr. Postman will eventually
6164
deliver the mail to your recipient. Using this metaphor RabbitMQ is a post box,
6265
post office and a postman.
6366

@@ -68,7 +71,7 @@ blobs of data - _messages_.
6871
RabbitMQ uses a weird jargon, but it's simple once you'll get it. For example:
6972

7073
* _Producing_ means nothing more than sending. A program that sends messages
71-
is a _producer_.
74+
is a _producer_. We'll draw it like that, with "P":
7275

7376
{% dot -Gsize="10,0.3" -Grankdir=LR%}
7477
digraph G {
@@ -82,7 +85,8 @@ RabbitMQ uses a weird jargon, but it's simple once you'll get it. For example:
8285
is not bound by any limits, it can store how many messages you
8386
like - it's essentially an infinite buffer. Many _producers_ can send
8487
messages that go to the one queue, many _consumers_ can try to
85-
receive data from one _queue_.
88+
receive data from one _queue_. Queue'll be drawn as like that, with
89+
its name above it:
8690

8791
{% dot -Gsize="10,0.9" -Grankdir=LR%}
8892
digraph G {
@@ -95,7 +99,7 @@ RabbitMQ uses a weird jargon, but it's simple once you'll get it. For example:
9599
{% enddot %}
96100

97101
* _Consuming_ has a simmilar meaning to receiving. _Consumer_ is a program
98-
that mostly waits to receive messages.
102+
that mostly waits to receive messages. On our drawings it's shown with "C":
99103

100104
{% dot -Gsize="10,0.3" -Grankdir=LR %}
101105
digraph G {
@@ -127,6 +131,9 @@ digraph G {
127131
}
128132
{% enddot %}
129133

134+
Producer sends messages to the "test" queue. The consumer receives
135+
messages from that queue.
136+
130137
> #### RabbitMQ libraries
131138
>
132139
> RabbitMQ speaks AMQP protocol. To use Rabbit you'll need a library that
@@ -171,7 +178,8 @@ digraph G {
171178

172179

173180
Our first program `send.py` will send a single message to the queue.
174-
The first thing we need to do is connect to RabbitMQ server.
181+
The first thing we need to do is to establish a connection with
182+
RabbitMQ server.
175183

176184
{% highlight python %}
177185
#!/usr/bin/env python
@@ -183,28 +191,27 @@ The first thing we need to do is connect to RabbitMQ server.
183191
channel = connection.channel()
184192
{% endhighlight %}
185193

186-
187-
Whenever we send a message we need to make sure the recipient queue exists.
188-
RabbitMQ will just trash the message if can't deliver it. So, we need to
189-
create a queue to which the message will be delivered. Let's name this queue
190-
_test_:
194+
We're connected now. Next, before sending we need to make sure the
195+
recipient queue exists. If we send a message to non-existing location,
196+
RabbitMQ will just trash the message. Let's create a queue to which
197+
the message will be delivered, let's name it _test_:
191198

192199
{% highlight python %}
193200
channel.queue_declare(queue='test')
194201
{% endhighlight %}
195202

196203

197204
At that point we're ready to send a message. Our first message will
198-
contain a string _Hello World!_ and we want to send it to our _test_
199-
queue.
205+
just contain a string _Hello World!_, we want to send it to our
206+
_test_ queue.
200207

201-
In RabbitMQ a message never goes directly to the queue, it always
208+
In RabbitMQ a message never can be send directly to the queue, it always
202209
needs to go through an _exchange_. But let's not get dragged by the
203-
details - you can read more about _exchanges_ in third part of this
204-
tutorial. All we need to know now is how to use a default exchange
205-
identified by an empty string. That exchange is a special one that
210+
details - you can read more about _exchanges_ in [third part of this
211+
tutorial]({{ python_three_url }}). All we need to know now is how to use a default exchange
212+
identified by an empty string. This exchange is special - it
206213
allows us to specify exactly to which queue the message should go.
207-
The queue name is specified by the `routing_key` variable:
214+
The queue name needs to be specified in the `routing_key` parameter:
208215

209216
{% highlight python %}
210217
channel.basic_publish(exchange='',
@@ -236,28 +243,27 @@ digraph G {
236243
Our second program `receive.py` will receive messages from the queue and print
237244
them on the screen.
238245

239-
The code responsible for connecting to Rabbit is the same as the previous example.
240-
You can copy the first 7 lines.
246+
Again, first we need to connect to RabbitMQ server. The code
247+
responsible for connecting to Rabbit is the same as previously.
241248

242-
# ... connection code is the same, copy first 7 lines from send.py ...
243-
244-
Just like before, in the beginning we must make sure that the
245-
queue exists. Creating a queue using `queue_declare` is idempotent - you can
246-
run the command as many times you like, and only one queue will be created.
249+
Next step, just like before, is to make sure that the
250+
queue exists. Creating a queue using `queue_declare` is idempotent - we can
251+
run the command as many times you like, and only one will be created.
247252

248253
{% highlight python %}
249254
channel.queue_declare(queue='test')
250255
{% endhighlight %}
251256

252-
You may ask why to declare queue again - we have already declared it
253-
in our previous code. We could have avoided that if we always run the
254-
`send.py` program before this one. But we're not sure yet which
257+
You may ask why to declare the queue again - we have already declared it
258+
in our previous code. We could have avoided that if we were sure
259+
that the queue already exists. For example if `send.py` program was
260+
runned before. But we're not yet sure which
255261
program to run as first. In such case it's a good practice to repeat
256262
declaring the queue in both programs.
257263

258264
> #### Listing queues
259265
>
260-
> Sometimes you may want to see what queues does RabbitMQ store and how many
266+
> You may want to see what queues does RabbitMQ store and how many
261267
> messages are in them. You can do it using the `rabbitmqctl` tool:
262268
>
263269
> $ sudo rabbitmqctl list_queues
@@ -267,18 +273,20 @@ declaring the queue in both programs.
267273

268274

269275

270-
Receiving messages from the queue is a bit more complex. Whenever we receive
271-
a message, a `callback` function is called. In our case
272-
this function will print on the screen the contents of the message.
276+
Receiving messages from the queue is more complex. It works by subscribing
277+
a `callback` function to a queue. Whenever we receive
278+
a message, this `callback` function is called by the Pika library.
279+
In our case this function will print on the screen the contents of
280+
the message.
273281

274282
{% highlight python %}
275283
def callback(ch, method, header, body):
276284
print " [x] Received %.20r" % (body,)
277285
{% endhighlight %}
278286

279287

280-
Next, we need to tell RabbitMQ that this particular callback function is
281-
interested in messages from our _test_ queue:
288+
Next, we need to tell RabbitMQ that this particular callback function should
289+
receive messages from our _test_ queue:
282290

283291
{% highlight python %}
284292
channel.basic_consume(callback,
@@ -290,6 +298,8 @@ For that command to succeed we must be sure that a queue which we want
290298
to subscribe to exists. Fortunately we're confident about that - we've
291299
created a queue above - using `queue_declare`.
292300

301+
The `no_ack` parameter will be described [later on]({{ python_two_url }}).
302+
293303
And finally, we enter a never-ending loop that waits for data and runs callbacks
294304
whenever necessary.
295305

@@ -320,7 +330,7 @@ Full code for `send.py`:
320330
body='Hello World!')
321331
print " [x] Sent 'Hello World!'"
322332
{% endhighlight %}
323-
[(full send.py source)]({{ examples_url }}/python/send.py)
333+
[(send.py source)]({{ examples_url }}/python/send.py)
324334

325335

326336
Full `receive.py` code:
@@ -347,8 +357,7 @@ Full `receive.py` code:
347357

348358
pika.asyncore_loop()
349359
{% endhighlight %}
350-
351-
[(full receive.py source)]({{ examples_url }}/python/receive.py)
360+
[(receive.py source)]({{ examples_url }}/python/receive.py)
352361

353362

354363
Now we can try out our programs. First, let's send a message using our
@@ -364,9 +373,9 @@ Let's receive it:
364373
[x] Received 'Hello World!'
365374

366375
Hurray! We were able to send our first message through RabbitMQ. As you might
367-
have noticed, the `receive.py` program didn't exit. It will stay ready to
368-
receive further messages. Try to run `send.py` in a new terminal!
376+
have noticed, the `receive.py` program doesn't exit. It will stay ready to
377+
receive further messages. Try to r 5458 un `send.py` again in a new terminal!
369378

370379
We've learned how to send and receive a message from a named
371-
queue. It's time to move on to part 2 of this tutorial and build a
372-
simple _task queue_.
380+
queue. It's time to move on to [part 2]({{ python_two_url }})
381+
and build a simple _task queue_.

0 commit comments

Comments
 (0)
0