Skip to content

Commit 665350d

Browse files
authored
fix: handle task callback correctly (#22)
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).
1 parent 0b71933 commit 665350d

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

example_publisher/providers/coin_gecko.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def upd_products(self, product_symbols: List[Symbol]) -> None:
3030
new_prices[id] = self._prices.get(id, None)
3131
else:
3232
raise ValueError(
33-
f"{coin_gecko_product.symbol} not found in available products"
33+
f"{coin_gecko_product.symbol} not found in available products" # noqa: E713
3434
)
3535

3636
self._prices = new_prices

example_publisher/pythd.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ def __init__(
4343
self.address = address
4444
self.server: Server = None
4545
self.on_notify_price_sched = on_notify_price_sched
46-
self._notify_price_sched_tasks = set()
46+
self._tasks = set()
4747

4848
async def connect(self) -> Server:
4949
self.server = Server(self.address)
5050
self.server.notify_price_sched = self._notify_price_sched
5151
task = await self.server.ws_connect()
5252
task.add_done_callback(Pythd._on_connection_done)
53+
self._tasks.add(task)
5354

5455
@staticmethod
5556
def _on_connection_done(task):
@@ -73,8 +74,8 @@ def _notify_price_sched(self, subscription: int) -> None:
7374
task = asyncio.get_event_loop().create_task(
7475
self.on_notify_price_sched(subscription)
7576
)
76-
self._notify_price_sched_tasks.add(task)
77-
task.add_done_callback(lambda: self._notify_price_sched_tasks.remove(task))
77+
self._tasks.add(task)
78+
task.add_done_callback(self._tasks.discard)
7879

7980
async def all_products(self) -> List[Product]:
8081
result = await self.server.get_product_list()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "example-publisher"
3-
version = "1.0.2"
3+
version = "1.0.3"
44
description = ""
55
authors = []
66
license = "Apache-2"

0 commit comments

Comments
 (0)