镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Unclear how to resume iterating with PageIterator if the callback returns False #688

Description

In other SDKs, the iterator class typically has a Resume method to restart the iteration where it left off, and a corresponding State property to check if the iterator has completed or not. For example, in .NET:

var pageIterator = PageIterator<Message, MessageCollectionResponse>
    .CreatePageIterator(
        graphClient,
        messages,
        (msg) =>
        {
            Console.WriteLine(msg.Subject);
            count++;
            // If we've iterated over the limit,
            // stop the iteration by returning false
            return count < pauseAfter;
        });

await pageIterator.IterateAsync();

while (pageIterator.State != PagingState.Complete)
{
    Console.WriteLine("Iteration paused for 5 seconds...");
    await Task.Delay(5000);
    // Reset count
    count = 0;
    await pageIterator.ResumeAsync();
}

Is there a way to do this in this SDK?

Activity

  1. jasonjoh commented on Sep 23, 2024

    @jasonjoh
    MemberAuthor

    For context, this is so I can add Python snippets to this documentation: https://learn.microsoft.com/en-us/graph/sdks/paging

  2. jasonjoh commented on Sep 24, 2024

    @jasonjoh
    MemberAuthor

    I've done some experimentation, and I found that after a pause, I can just call iterate again to start back up from where it left off. However, there's no good way that I can see for my code to detect that it should stop calling iterate. Once the iterator gets to the last page from the server, my code just continually iterates over that page.

    while True:
        # The callback here returns false after iterating over 25 messages
        await page_iterator.iterate(Paging.message_callback_with_pause)
    
        # Ideally I could test something here and break out of the loop
    
        time.sleep(5)
  3. sanmai-NL commented on Jan 8, 2025

    @sanmai-NL

    Jason Johnston (@jasonjoh) Can you use the has_next attribute of PageIterator under Python?

  4. sanmai-NL commented on Jan 8, 2025

    @sanmai-NL

    I seems like has_next never gets updated. And I also end up in loop with PageResult.odata_next_link staying the same.

  5. sanmai-NL commented on Feb 1, 2025

    @sanmai-NL

    Shem Ogumbe (@shemogumbe) As author of the page iterator PR, can you please check whether my previous comment arises from a defect?

  6. added
    priority:p2Medium. For a p2 bug, generally have a work-around. Bug SLA <=30 days
    bugSomething isn't working
    and removed on Feb 4, 2025
  7. ashikns commented on Aug 5, 2025

    @ashikns

    A workaround I've found is to check bool(iterator.current_page.odata_next_link) instead of iterator.has_next. I think the problem is in PageIterator Init self.has_next = bool(page.odata_next_link). I haven't tested it extensively, but a loop like this works for my purpose:

        memberOf = await graphClient.users.by_user_id(email).member_of.get()
        iterator = PageIterator(memberOf, graphClient.request_adapter)
        temp = []
    
        def process_item(item)-> bool:
            if item and hasattr(item, 'id'):
                temp.append(item.id)
            return True
    
        while bool(iterator.current_page.odata_next_link):
            await iterator.iterate(process_item)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpriority:p2Medium. For a p2 bug, generally have a work-around. Bug SLA <=30 days

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions