8000 4 spaces, not 5, are used for code. · seanhuang/rabbitmq-tutorials@5039d03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5039d03

Browse files
committed
4 spaces, not 5, are used for code.
1 parent 4b5f26e commit 5039d03

File tree

2 files changed

+62
-54
lines changed

2 files changed

+62
-54
lines changed

python/tutorial-one.md

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ Our overall design will look like:
131131
>
132132
> * On Ubuntu:
133133
>
134-
> $ sudo apt-get install python-pip
134+
> $ sudo apt-get install python-pip
135135
>
136136
> * On Debian:
137137
>
138-
> $ sudo apt-get install python-setuptools
139-
> $ sudo easy_install pip
138+
> $ sudo apt-get install python-setuptools
139+
> $ sudo easy_install pip
140140
141141
### Sending
142142

@@ -150,31 +150,31 @@ Our overall design will look like:
150150
Our first program `send.py` will send a single message to the queue.
151151
The first thing we need to do is connect to RabbitMQ server.
152152

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>
161164

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">&#39;127.0.0.1&#39;</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">&#39;guest&#39;</span><span class="p">,</span> <span class="s">&#39;guest&#39;</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>
168165

169166

170167
Whenever we send a message we need to make sure the recipient queue exists.
171168
RabbitMQ will just trash the message if can't deliver it. So, we need to
172169
create a queue to which the message will be delivered. Let's name this queue
173170
_test_:
174171

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">&#39;test&#39;</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+
178178

179179

180180
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
189189
allows us to specify exactly to which queue the message should go.
190190
The queue name is specified by the `routing_key` variable:
191191

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

202202

203203
[(full send.py source)](http://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/send.py)
@@ -218,15 +218,18 @@ them on the screen.
218218
The code responsible for connecting to Rabbit is the same as the previous example.
219219
You can copy the first 7 lines.
220220

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 ...
222222

223223
Just like before, in the beginning we must make sure that the
224224
queue exists. Creating a queue using `queue_declare` is idempotent - you can
225225
run the command as many times you like, and only one queue will be created.
226226

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">&#39;test&#39;</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+
230233

231234
You may ask why to declare queue again - we have already declared it
232235
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
239242
a message, a `callback` function is called. In our case
240243
this function will print on the screen the contents of the message.
241244

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">&quot; [x] Received </span><span class="si">%.20r</span><span class="s">&quot;</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 &quot; [x] Received %.20r&quot; % (body,)</code>
249+
</pre>
250+
</div>
251+
247252

248253

249254
Next, we need to tell RabbitMQ that this particular callback function is
250255
interested in messages from our _test_ queue:
251256

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">&#39;test&#39;</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+
259265

260266
For that command to succeed we must be sure that a queue which we want
261267
to subscribe to exists. Fortunately we're confident about that - we've
@@ -264,11 +270,13 @@ created a queue above - using `queue_declare`.
264270
And finally, we enter a never-ending loop that waits for data and runs callbacks
265271
whenever necessary.
266272

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">&#39; [*] Waiting for messages. To exit press CTRL+C&#39;</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+
272280

273281
[(full receive.py source)](http://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/receive.py)
274282

python/tutorial-one.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ digraph G {
143143
>
144144
> * On Ubuntu:
145145
>
146-
> $ sudo apt-get install python-pip
146+
> $ sudo apt-get install python-pip
147147
>
148148
> * On Debian:
149149
>
150-
> $ sudo apt-get install python-setuptools
151-
> $ sudo easy_install pip
150+
> $ sudo apt-get install python-setuptools
151+
> $ sudo easy_install pip
152152

153153
### Sending
154154

@@ -244,7 +244,7 @@ them on the screen.
244244
The code responsible for connecting to Rabbit is the same as the previous example.
245245
You can copy the first 7 lines.
246246

247-
# ... connection code is the same, copy first 7 lines from send.py ...
247+
# ... connection code is the same, copy first 7 lines from send.py ...
248248

249249
Just like before, in the beginning we must make sure that the
250250
queue exists. Creating a queue using `queue_declare` is idempotent - you can

0 commit comments

Comments
 (0)
0