GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow easy paging for list operations · Issue #895 · googleapis/google-cloud-python

Allow easy paging for list operations #895

New issue
Copy link
New issue
Copy link
Closed
Closed
Allow easy paging for list operations#895
Copy link
Assignees
jgeewax
Labels
api: datastoreIssues related to the Datastore API.api: pubsubIssues related to the Pub/Sub API.api: storageIssues related to the Cloud Storage API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@jgeewax
jgeewax
opened on May 26, 2015
Issue body actions

Taking the comment on https://github.com/GoogleCloudPlatform/gcloud-python/pull/889/files#r31042158 over here.

Requiring explicit paging for topics is really ugly, there must be a better way than:

all_topics = []

topics, token = client.list_topics()
all_topics.extend(topics)
while token is not None:
  topics, token = client.list_topics(page_token=token)
  all_topics.extend(topics)

If our concern is "people could make a boatload of requests and they don't realize it", we could always limit this like we do with the recursive deleting in storage?

topics = client.list_topics(limit=100)

We can do a bunch of this with the page_size parameter, but that might mean that I have to wait for everything to come back before starting any work, which seems kind of ridiculous.

It'd be really nice if that limit and page-size stuff was both there, so it's easy to do things like "I want the first 5000 topics, and I want to pull them from the server in chunks of 50":

for topic in client.list_topics(page_size=50, limit=5000):
  push_work_to_other_system(topic)

To add a bit more context, I'd like to toss out: what if we made all of our list operations return iterators?

The use cases I see here are...

  1. I want everything, give it all to me (for topic in list_topics())
  2. I want up to N things, stop giving them to me then (for topic in list_topics(limit=100))
  3. I don't know how many I want, I'll know when I want to stop though... (for topics in list_topics(): if topic.name == 'foo': break)
  4. Combination of the previous two (I don't know when I want to stop, but don't let me go on forever, kill it at say... 1000)
  5. I want to pick up where I left off, I saved a token somewhere (sort of like offset)! (for topic in list_topics(page_token=token, limit=100))

The "let's just always return page, page_token" thing doesn't really make all of those use-cases all that fun... But if we always return iterators, they are all easy.

Further, let's say I have a weird case where I just want one page worth of stuff... list_topics().get_current_page() could return what you want, no?

Activity

Next
  1. jgeewax
    added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    api: datastoreIssues related to the Datastore API.
    api: storageIssues related to the Cloud Storage API.
    api: pubsubIssues related to the Pub/Sub API.
    on May 26, 2015
  2. jgeewax
    added this to the This milestone has been deleted milestone on May 26, 2015
  3. dhermes commented on Jun 6, 2015

    @dhermes
    dhermes
    on Jun 6, 2015
    Contributor
    More actions

    In general I like the idea, but was likely intimidated due to the sheer length of what you wrote.

    Recall #90 was in place just in case this was necessary. We have a nice Iterator matching most of this pattern in datastore.query.

  4. jgeewax commented on Jun 6, 2015

    @jgeewax
    jgeewax
    on Jun 6, 2015
    ContributorAuthor
    More actions

    Sorry about that. In short, I want to write:

    for topic in client.list_topics(page_size=50, limit=5000):
      do_something_with(topic)

    with the ability to short-circuit out (break or return).

  5. dhermes commented on Jul 7, 2015

    @dhermes
    dhermes
    on Jul 7, 2015
    Contributor
    More actions

    @tseaver Do you want to take a stab at this in pubsub?

  6. jgeewax commented on Jul 7, 2015

    @jgeewax
    jgeewax
    on Jul 7, 2015 via email
    ContributorAuthor
    More actions
    Should be pretty easy now that we have gcloud.iterator.Iterator
  7. dhermes commented on Jul 7, 2015

    @dhermes
    dhermes
    on Jul 7, 2015
    Contributor
    More actions

    Indeed. I am moving the iterator in storage into the client right now (for #952) and noticed we had no iterator in pubsub.

  8. dhermes commented on Aug 27, 2015

    @dhermes
    dhermes
    on Aug 27, 2015
    Contributor
    More actions

    Is this not addressed?

  9. jgeewax commented on Aug 28, 2015

    @jgeewax
    jgeewax
    on Aug 28, 2015
    ContributorAuthor
    More actions

    I just looked at documentation for pubsub and storage, both seem to use the iterator, so I'm happy.

    I'll open a separate issue if I see anything else.

  10. jgeewax
    closed this as completedon Aug 28, 2015
  11. jgeewax
    reopened this on Apr 5, 2016
  12. jgeewax commented on Apr 5, 2016

    @jgeewax
    jgeewax
    on Apr 5, 2016
    ContributorAuthor
    More actions

    Yea, this isn't actually fixed.

    See https://github.com/GoogleCloudPlatform/gcloud-python/blob/188515f192e3823ce21a47df2d4742995d08f109/gcloud/pubsub/client.py#L47

    I think we're making a mistake returning a tuple here, and instead should return an iterator.

    This would mean that we can treat it as a list (not giving a shit about pagination -- aka, good for scripts that need to go through all topics) as well as a paged-resource if needed.

    Infiniscroll list

    for topic in client.list_topics():
      do_something_with_topic(topic)

    Explicitly paginated

    token = request.GET['token']
    topics = client.list_topics(next_page_token=token)
    html = template.render({'token': topics.next_page_token, 'topics': topics.page})
    response.write(html)

    The reason I care so much about this is that the first case is incredibly common when people first start using this library... So I want to make the common (intro) case fast.

    Compared to today:

    Infiniscroll list

    token = None
    while True:
      topics, token = client.list_topics(next_page_token=token):
        for topic in topics:
          do_something_with_topic(topic)
        if token is None:
          break
    
    # or maybe...
    
    token = True
    while token is not None:
      topics, token = client.list_topics(next_page_token=token)
        for topic in topics:
          do_something_with_topic(topic)

    (correct me if there's a better way?)

    Explicitly paginated

    token = request.GET('page_token', None)
    topics, token = client.list_topics(next_page_token=token)
    html = template.render({'page_token': token, 'topics': topics})
    response.write(html)

    Note that the paginated version doesn't really change all that much, but the infiniscroll version looks way worse off.... It forces people to learn about page tokens when they aren't interested in paginating anything themselves. They just want to iterate over all the stuff.

  13. 21 remaining items

  14. theacodes commented on Oct 3, 2016

    @theacodes
    theacodes
    on Oct 3, 2016
    Contributor
    More actions

    I agree that we should have a simple iterate that iterates over everything, however, I want to make sure we don't remove the ability to manually page. This use case is extremely important for datastore where the pagination can happen across multiple user requests.

  15. daspecster
    added
    blocks-docs-samples
    on Oct 3, 2016
  16. tseaver commented on Oct 3, 2016

    @tseaver
    tseaver
    on Oct 3, 2016
    Contributor
    More actions

    My $0.02 on the strategy:

    • Rename the current paging methods, from list_foo -> list_foo_paged.
    • Add sibling list_foo methods which just return an iterator already wrapped around list_foo_paged. These methods do not carry params for all the possible iterator properties to list_foo: instead, users who care can configure them before beginning to iterate.
    Reacted by Danny Hermes
  17. dhermes commented on Oct 25, 2016

    @dhermes
    dhermes
    on Oct 25, 2016
    Contributor
    More actions

    FYI I am really close to done

  18. tseaver commented on Nov 15, 2016

    @tseaver
    tseaver
    on Nov 15, 2016
    Contributor
    More actions

    0.21 releases include the new iterators.

  19. tseaver
    closed this as completedon Nov 15, 2016
  20. dhermes commented on Nov 15, 2016

    @dhermes
    dhermes
    on Nov 15, 2016
    Contributor
    More actions

    @tseaver There are still some list_ type methods in BigQuery and Monitoring that need to be changed over.

  21. JustinBeckwith
    assigned
    jgeewax
    on Feb 1, 2021
  22. parthea
    added a commit that references this issue on Aug 21, 2025

    chore(python): updates jinja template to control blank line whitespace (

    08ba961
  23. parthea
    added 2 commits that reference this issue on Sep 16, 2025

    chore(python): updates jinja template to control blank line whitespace (

    21492c6

    chore(deps): update all dependencies (#895)

    c8c230b
  24. parthea
    added 2 commits that reference this issue on Nov 22, 2025

    build: update actions/checkout and actions/setup-python (#895)

    315056f

    build(deps): [autoapprove] bump cryptography from 41.0.0 to 41.0.2 (#895

    08df185
  25. parthea
    added 2 commits that reference this issue on Mar 2, 2026

    CI: Add typed_flaky to streaming_pull system tests (#895)

    682a7b4

    test: Add test of datetime and time pandas load (#895)

    d6d2914
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @jgeewax
    jgeewax

Labels

api: datastoreIssues related to the Datastore API.api: pubsubIssues related to the Pub/Sub API.api: storageIssues related to the Cloud Storage API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.