Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list(bucket()) hangs with 100% CPU #370

Closed
alexchandel opened this issue Jan 3, 2020 · 3 comments
Closed

list(bucket()) hangs with 100% CPU #370

alexchandel opened this issue Jan 3, 2020 · 3 comments

Comments

@alexchandel
Copy link

From the docs:

b = bucket(['a1', 'b1', 'c1', 'a2', 'b2', 'c2', 'b3'], lambda x: x[0])

The following uses 100% CPU and never returns:

list(b)

Additionally, dict(b) fails with dictionary update sequence element #0 has length 0; 2 is required, leaving no way to inspect the opaque result without prior knowledge of all keys.

This occurs in all versions of more-itertools.

@MSeifert04
Copy link
Contributor

bucket itself is an infinite iterable. That's why it hangs.

However maybe bucket is only iterable by accident (it implements __getitem__ so it's iterable) - I'm not sure.

But yeah, bucket (as it currently is) isn't meant to be iterated over without knowledge of the keys.

@bbayles
Copy link
Collaborator

bbayles commented Jan 4, 2020

Correct - this was discussed at the time of implementation.

@bbayles
Copy link
Collaborator

bbayles commented Jan 4, 2020

Because __getitem__ is implemented, when calling list() or dict() on bucket(something) causes Python to look up 0, 1,, 2... on to infinity.

#371 prevents this by explicitly defining __iter__. Now calling list() on bucket(something) will extract the keys from the iterable and return them.

@bbayles bbayles closed this as completed Jan 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants