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

Don't create coroutines before they're needed when loading items #27

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- Once the first viewed tab has loaded, other tabs will start to load in the
background (one after the other) as the user reads the first.
- Added a config option to turn off the above.
- Fixed a non-awaited-coroutine warning that could happen when quitting
while loading items. ([#26](https://github.com/davep/oshit/issues/26))

## 0.10.0

Expand Down
16 changes: 11 additions & 5 deletions oshit/hn/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,19 @@ async def _items_from_ids(
"""
concurrency_limit = Semaphore(self._max_concurrency)

async def limited(coroutine: Awaitable[ItemType]) -> ItemType:
async def item(item_id: int) -> ItemType:
"""Get an item, with a limit on concurrent requests.

Args:
item_id: The ID of the item to get.

Returns:
The item.
"""
async with concurrency_limit:
return await coroutine
return await self.item(item_type, item_id)

return await gather(
*[limited(self.item(item_type, item_id)) for item_id in item_ids]
)
return await gather(*[item(item_id) for item_id in item_ids])

async def _id_list(self, list_type: str, max_count: int | None = None) -> list[int]:
"""Get a given ID list.
Expand Down