none
Azure SDK for Python | peek() doesn't work RRS feed

  • Question

  • Hello,

    When I try to peek at messages in a topic subscription with a count of say, five, I can only get the first two messages although the subscription has 10+ messages. Here's what I'm trying:

    subscription_client = SubscriptionClient.from_connection_string(CONNECTION_STR
    , name=SUBSCRIPTION_NAME, topic=TOPIC_NAME)
    
    with subscription_client.get_receiver() as receiver:   
        messages = receiver.peek(count=5,start_from=None)
        for message in messages:  
             print(message.message,"\n")

    I also noticed that for subscriptions with 100+ messages, I could only peek at first four messages.

    Please help.


    • Edited by Bhaveek Desai Monday, May 27, 2019 7:43 PM typo correction
    Monday, May 27, 2019 7:43 PM

Answers

  • Based on your observations, I believe the topic you are using is partitioned. If so, this is a known and documented limitation.

    You should instead be able to fetch the number of messages you require like this

    messages=[]
    x=0
    index=None
    with subscription_client.get_receiver() as receiver:
        while (x < 15):
            newMessages = receiver.peek(count=15-x, start_from=index)
            messages += newMessages
            x += len(newMessages)
            index=messages[len(messages)-1].sequence_number+1
    
    for message in messages:
        print(message.message,"\n")

    You will also notice that the sequence numbers wouldn't be in order due to the partitioning.


    • Marked as answer by Bhaveek Desai Thursday, May 30, 2019 2:19 PM
    Wednesday, May 29, 2019 12:03 PM
    Moderator

All replies

  • Based on your observations, I believe the topic you are using is partitioned. If so, this is a known and documented limitation.

    You should instead be able to fetch the number of messages you require like this

    messages=[]
    x=0
    index=None
    with subscription_client.get_receiver() as receiver:
        while (x < 15):
            newMessages = receiver.peek(count=15-x, start_from=index)
            messages += newMessages
            x += len(newMessages)
            index=messages[len(messages)-1].sequence_number+1
    
    for message in messages:
        print(message.message,"\n")

    You will also notice that the sequence numbers wouldn't be in order due to the partitioning.


    • Marked as answer by Bhaveek Desai Thursday, May 30, 2019 2:19 PM
    Wednesday, May 29, 2019 12:03 PM
    Moderator
  • Thank you so much Pramod, this worked!
    Thursday, May 30, 2019 2:20 PM