GeezerVison is showing github.com — Allow easy paging for list operations · Issue #895 · googleapis/google-cloud-python
Allow easy paging for list operations #895DescriptionTaking 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...
- I want everything, give it all to me (
for topic in list_topics())
- I want up to N things, stop giving them to me then (
for topic in list_topics(limit=100))
- 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)
- 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)
- 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? Activityadded this to the This milestone has been deleted milestone on May 26, 2015dhermes commented on Jun 6, 2015 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. jgeewax commented 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). dhermes commented on Jul 7, 2015 @tseaver Do you want to take a stab at this in pubsub? jgeewax commented on Jul 7, 2015 ContributorAuthor More actions Should be pretty easy now that we have gcloud.iterator.Iterator dhermes commented on Jul 7, 2015 Indeed. I am moving the iterator in storage into the client right now (for #952) and noticed we had no iterator in pubsub. dhermes commented on Aug 27, 2015 jgeewax commented 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. jgeewax commented 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. theacodes commented on Oct 3, 2016 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. tseaver commented on Oct 3, 2016 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.
dhermes commented on Oct 25, 2016 FYI I am really close to done tseaver commented on Nov 15, 2016 0.21 releases include the new iterators.
dhermes commented on Nov 15, 2016 @tseaver There are still some list_ type methods in BigQuery and Monitoring that need to be changed over.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|