Skip to content

Commit

Permalink
fix: handle task callback correctly (#22)
Browse files Browse the repository at this point in the history
The existing code would crash on calling the callback lambda for
a completed task because it takes `task` as parameter and our lambda
had no input. This change just passes discard that takes an element as
an argument. Discard is chosen over remove to ensure it won't raise
error (although it shouldn't happen).
  • Loading branch information
ali-bahjati authored Feb 14, 2024
1 parent 0b71933 commit 665350d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion example_publisher/providers/coin_gecko.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def upd_products(self, product_symbols: List[Symbol]) -> None:
new_prices[id] = self._prices.get(id, None)
else:
raise ValueError(
f"{coin_gecko_product.symbol} not found in available products"
f"{coin_gecko_product.symbol} not found in available products" # noqa: E713
)

self._prices = new_prices
Expand Down
7 changes: 4 additions & 3 deletions example_publisher/pythd.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ def __init__(
self.address = address
self.server: Server = None
self.on_notify_price_sched = on_notify_price_sched
self._notify_price_sched_tasks = set()
self._tasks = set()

async def connect(self) -> Server:
self.server = Server(self.address)
self.server.notify_price_sched = self._notify_price_sched
task = await self.server.ws_connect()
task.add_done_callback(Pythd._on_connection_done)
self._tasks.add(task)

@staticmethod
def _on_connection_done(task):
Expand All @@ -73,8 +74,8 @@ def _notify_price_sched(self, subscription: int) -> None:
task = asyncio.get_event_loop().create_task(
self.on_notify_price_sched(subscription)
)
self._notify_price_sched_tasks.add(task)
task.add_done_callback(lambda: self._notify_price_sched_tasks.remove(task))
self._tasks.add(task)
task.add_done_callback(self._tasks.discard)

async def all_products(self) -> List[Product]:
result = await self.server.get_product_list()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "example-publisher"
version = "1.0.2"
version = "1.0.3"
description = ""
authors = []
license = "Apache-2"
Expand Down

0 comments on commit 665350d

Please sign in to comment.