@@ -46,7 +46,7 @@ def configurate_queue(self, **kwargs):
4646 if not self .queue :
4747 self .queue = kwargs .get ('queue' , '' )
4848 self .basic_ack = kwargs .get ('basic_ack' , True )
49- self .prefetch_count = kwargs .get ('prefetch_count' , 0 )
49+ self .prefetch_count = kwargs .get ('prefetch_count' , 1 )
5050
5151 self .channel .queue_declare (
5252 queue = self .queue ,
@@ -64,7 +64,7 @@ def configurate_queue(self, **kwargs):
6464 self .queue , self .basic_ack , self .prefetch_count )
6565
6666 def connect (self ):
67- """Connect usgin BlockingConnection.
67+ """Connect to AMQP server usgin BlockingConnection.
6868
6969 """
7070 try :
@@ -108,7 +108,7 @@ def format_message(self, message):
108108 _message = {}
109109 _message ['body' ] = message ['body' ]
110110 _message ['routing_key' ] = self .queue
111- _message ['exchange' ] = exchange
111+ _message ['exchange' ] = exchange
112112 _message ['properties' ] = pika .BasicProperties (
113113 content_type = 'application/json' ,
114114 delivery_mode = delivery_mode ,
@@ -124,8 +124,8 @@ def consume(self, worker):
124124 :param function worker: Method that consume the message
125125
126126 """
127- self . _consume_worker = worker
128- self .channel .basic_consume (self . consume_callback , self .queue )
127+ callback = self . consume_callback ( worker )
128+ self .channel .basic_consume (callback , self .queue )
129129
130130 try :
131131 self .channel .start_consuming ()
@@ -134,24 +134,40 @@ def consume(self, worker):
134134 self .channel .stop_consuming ()
135135 self .close ()
136136
137- def consume_callback (self , channel , method , properties , body ):
138- """Message consume callback
137+ def consume_callback (self , worker ):
138+ """Decorate worker to exectue on consume callback.
139139
140- :param pika.channel.Channel channel: The channel object
141- :param pika.Spec.Basic.Deliver method: basic_deliver method
142- :param pika.Spec.BasicProperties properties: properties
143- :param str|unicode body: The message body
140+ :param function worker: Worker to execture in the consume callback
144141
145142 """
146- self . _consume_worker (channel , method , properties , body )
147- self . consume_acknowledge ( channel , method . delivery_tag )
143+ def callback (channel , method , properties , body ):
144+ """Message consume callback.
148145
149- def consume_acknowledge (self , channel , tag ):
150- """Message acknowledge
146+ :param pika.channel.Channel channel: The channel object
147+ :param pika.Spec.Basic.Deliver method: basic_deliver method
148+ :param pika.Spec.BasicProperties properties: properties
149+ :param str|unicode body: The message body
150+
151+ """
152+ # Execute the worker
153+ acknowledge = worker (channel , method , properties , body )
154+
155+ # Acknowledge the message or not
156+ self ._consume_acknowledge (channel , method .delivery_tag , acknowledge )
157+
158+ return callback
159+
160+ def _consume_acknowledge (self , channel , tag , acknowledge = True ):
161+ """Message acknowledge.
151162
152163 :param pika.channel.Channel channel: Channel to acknowledge the message
153164 :param int tag: Message tag to acknowledge
165+ :param bool acknowledge: If should acknowledge the message or not
154166
155167 """
168+ if acknowledge is False :
169+ channel .basic_nack (delivery_tag = tag )
170+ return
171+
156172 channel .basic_ack (delivery_tag = tag )
157173
0 commit comments