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

wrap tasks in a while loop to avoid exceptions outside the Try/Except block #281

Merged
merged 1 commit into from
Oct 5, 2020

Conversation

vincentsarago
Copy link
Member

No description provided.

@@ -301,7 +301,7 @@ def tile(
tile = mercantile.Tile(x=tile_x, y=tile_y, z=tile_z)
if not tile_exists(self.bounds, tile_z, tile_x, tile_y):
raise TileOutsideBounds(
"Tile {}/{}/{} is outside image bounds".format(tile_z, tile_x, tile_y)
f"Tile {tile_z}/{tile_x}/{tile_y} is outside {self.filepath} bounds"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a bit more info on this exception

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know I love my f-strings 😉

try:
future, asset = next(tasks) # type: ignore
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why mypy wasn't happy here

if isinstance(future, futures.Future):
yield future.result(), asset
else:
yield future, asset
except StopIteration:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we put StopIteration first, so someone doesn't try to put StopIteration as allowed_exceptions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They might wonder why their endless loop is slow...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well if they do that (putting StopIteration in allowed_exception) the while loop while never exit ...

Copy link
Member

@kylebarron kylebarron Oct 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha yes, hence the loop will never end and will be slow 😄

(executor.submit(reader, asset, *args, **kwargs), asset)
for asset in assets
]
return iter(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapping the futures inside an iterator to be able to use next

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might just be able to use a generator comprehension instead? Replace the [ ] with parentheses?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried and it didn't work 🤷‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think because the future needs to be executed with the excetutor. if you use a generator, the code will be run outide (in the filer function)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense 👍

@vincentsarago vincentsarago self-assigned this Oct 5, 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

Successfully merging this pull request may close these issues.

3 participants