4646import _pulsar
4747
4848from _pulsar import Result , CompressionType , ConsumerType , InitialPosition , PartitionsRoutingMode , BatchingType , \
49- LoggerLevel # noqa: F401
49+ LoggerLevel , BatchReceivePolicy # noqa: F401
5050
5151from pulsar .exceptions import *
5252
@@ -657,7 +657,8 @@ def subscribe(self, topic, subscription_name,
657657 replicate_subscription_state_enabled = False ,
658658 max_pending_chunked_message = 10 ,
659659 auto_ack_oldest_chunked_message_on_queue_full = False ,
660- start_message_id_inclusive = False
660+ start_message_id_inclusive = False ,
661+ batch_receive_policy = None
661662 ):
662663 """
663664 Subscribe to the given topic and subscription combination.
@@ -740,6 +741,8 @@ def my_listener(consumer, message):
740741 if autoAckOldestChunkedMessageOnQueueFull is true else it marks them for redelivery.
741742 start_message_id_inclusive: bool, default=False
742743 Set the consumer to include the given position of any reset operation like Consumer::seek.
744+ batch_receive_policy: class ConsumerBatchReceivePolicy
745+ Set the batch collection policy for batch receiving.
743746 """
744747 _check_type (str , subscription_name , 'subscription_name' )
745748 _check_type (ConsumerType , consumer_type , 'consumer_type' )
@@ -759,6 +762,7 @@ def my_listener(consumer, message):
759762 _check_type (int , max_pending_chunked_message , 'max_pending_chunked_message' )
760763 _check_type (bool , auto_ack_oldest_chunked_message_on_queue_full , 'auto_ack_oldest_chunked_message_on_queue_full' )
761764 _check_type (bool , start_message_id_inclusive , 'start_message_id_inclusive' )
765+ _check_type_or_none (ConsumerBatchReceivePolicy , batch_receive_policy , 'batch_receive_policy' )
762766
763767 conf = _pulsar .ConsumerConfiguration ()
764768 conf .consumer_type (consumer_type )
@@ -788,6 +792,8 @@ def my_listener(consumer, message):
788792 conf .max_pending_chunked_message (max_pending_chunked_message )
789793 conf .auto_ack_oldest_chunked_message_on_queue_full (auto_ack_oldest_chunked_message_on_queue_full )
790794 conf .start_message_id_inclusive (start_message_id_inclusive )
795+ if batch_receive_policy :
796+ conf .batch_receive_policy (batch_receive_policy .policy ())
791797
792798 c = Consumer ()
793799 if isinstance (topic , str ):
@@ -1237,6 +1243,20 @@ def receive(self, timeout_millis=None):
12371243 m ._schema = self ._schema
12381244 return m
12391245
1246+ def batch_receive (self ):
1247+ """
1248+ Batch receiving messages.
1249+
1250+ This calls blocks until has enough messages or wait timeout, more details to see {@link BatchReceivePolicy}.
1251+ """
1252+ messages = []
1253+ msgs = self ._consumer .batch_receive ()
1254+ for msg in msgs :
1255+ m = Message ()
1256+ m ._message = msg
1257+ messages .append (m )
1258+ return messages
1259+
12401260 def acknowledge (self , message ):
12411261 """
12421262 Acknowledge the reception of a single message.
@@ -1354,6 +1374,32 @@ def get_last_message_id(self):
13541374 """
13551375 return self ._consumer .get_last_message_id ()
13561376
1377+ class ConsumerBatchReceivePolicy :
1378+ """
1379+ Batch receive policy can limit the number and bytes of messages in a single batch,
1380+ and can specify a timeout for waiting for enough messages for this batch.
1381+
1382+ A batch receive action is completed as long as any one of the conditions (the batch has enough number
1383+ or size of messages, or the waiting timeout is passed) are met.
1384+ """
1385+ def __init__ (self , max_num_message , max_num_bytes , timeout_ms ):
1386+ """
1387+ Wrapper BatchReceivePolicy.
1388+
1389+ Parameters
1390+ ----------
1391+
1392+ max_num_message: Max num message, if less than 0, it means no limit. default: -1
1393+ max_num_bytes: Max num bytes, if less than 0, it means no limit. default: 10 * 1024 * 1024
1394+ timeout_ms: If less than 0, it means no limit. default: 100
1395+ """
1396+ self ._policy = BatchReceivePolicy (max_num_message , max_num_bytes , timeout_ms )
1397+
1398+ def policy (self ):
1399+ """
1400+ Returns the actual one BatchReceivePolicy.
1401+ """
1402+ return self ._policy
13571403
13581404class Reader :
13591405 """
0 commit comments