File tree Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Expand file tree Collapse file tree 2 files changed +13
-4
lines changed Original file line number Diff line number Diff line change 7
7
credentials = pika .PlainCredentials ('guest' , 'guest' )))
8
8
channel = connection .channel ()
9
9
10
+ channel .exchange_declare (exchange = 'logs' ,
11
+ type = 'fanout' )
12
+
10
13
message = ' ' .join (sys .argv [1 :]) or "info: Hello World!"
11
14
channel .basic_publish (exchange = 'logs' ,
12
15
routing_key = '' ,
Original file line number Diff line number Diff line change @@ -242,6 +242,9 @@ its value is ignored for `fanout` exchanges. Here goes the code for
242
242
credentials=pika.PlainCredentials('guest', 'guest')))
243
243
channel = connection.channel()
244
244
245
+ channel.exchange_declare(exchange='logs',
246
+ type='fanout')
247
+
245
248
message = ' '.join(sys.argv[1:]) or "info: Hello World!"
246
249
channel.basic_publish(exchange='logs',
247
250
routing_key='',
@@ -250,10 +253,13 @@ its value is ignored for `fanout` exchanges. Here goes the code for
250
253
{ % endhighlight % }
251
254
[(emit_log.py source)]({ { examples_url }} /python/emit_log.py)
252
255
253
- As you see, we avoided declaring exchange here. If the `logs` exchange
254
- isn't created at the time this code is executed the message will be
255
- lost. That's okay for us - if no consumer is listening yet (ie:
256
- the exchange hasn't been created) we can discard the message.
256
+ As you see, after establishing the connection we declared the
257
+ exchange. This step is neccesary as publishing to a non-existing
258
+ exchange is forbidden.
259
+
260
+ The messages will be lost if no queue is bound to the exchange yet,
261
+ but that's okay for us; if no consumer is listening yet (ie: the
262
+ exchange hasn't been created) we can safely discard the message.
257
263
258
264
The code for `receive_logs.py`:
259
265
You can’t perform that action at this time.
0 commit comments