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 77 credentials = pika .PlainCredentials ('guest' , 'guest' )))
88channel = connection .channel ()
99
10+ channel .exchange_declare (exchange = 'logs' ,
11+ type = 'fanout' )
12+
1013message = ' ' .join (sys .argv [1 :]) or "info: Hello World!"
1114channel .basic_publish (exchange = 'logs' ,
1215 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
242242 credentials=pika.PlainCredentials('guest', 'guest')))
243243 channel = connection.channel()
244244
245+ channel.exchange_declare(exchange='logs',
246+ type='fanout')
247+
245248 message = ' '.join(sys.argv[1:]) or "info: Hello World!"
246249 channel.basic_publish(exchange='logs',
247250 routing_key='',
@@ -250,10 +253,13 @@ its value is ignored for `fanout` exchanges. Here goes the code for
250253{ % endhighlight % }
251254[(emit_log.py source)]({ { examples_url }} /python/emit_log.py)
252255
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.
257263
258264The code for `receive_logs.py`:
259265
You can’t perform that action at this time.
0 commit comments