File tree Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env python
2
- import pika
2
+ import pika , sys , os
3
3
4
- connection = pika . BlockingConnection (
5
- pika .ConnectionParameters (host = 'localhost' ))
6
- channel = connection .channel ()
4
+ def main ():
5
+ connection = pika . BlockingConnection ( pika .ConnectionParameters (host = 'localhost' ))
6
+ channel = connection .channel ()
7
7
8
- channel .queue_declare (queue = 'hello' )
8
+ channel .queue_declare (queue = 'hello' )
9
9
10
+ def callback (ch , method , properties , body ):
11
+ print (" [x] Received %r" % body )
10
12
11
- def callback (ch , method , properties , body ):
12
- print (" [x] Received %r" % body )
13
+ channel .basic_consume (queue = 'hello' , on_message_callback = callback , auto_ack = True )
13
14
15
+ print (' [*] Waiting for messages. To exit press CTRL+C' )
16
+ channel .start_consuming ()
14
17
15
- channel .basic_consume (
16
- queue = 'hello' , on_message_callback = callback , auto_ack = True )
17
-
18
- print (' [*] Waiting for messages. To exit press CTRL+C' )
19
- channel .start_consuming ()
18
+ if __name__ == '__main__' :
19
+ try :
20
+ main ()
21
+ except KeyboardInterrupt :
22
+ print ('Interrupted' )
23
+ try :
24
+ sys .exit (0 )
25
+ except SystemExit :
26
+ os ._exit (0 )
You can’t perform that action at this time.
0 commit comments