Skip to content

Commit 3a5cea9

Browse files
Fix/update loop gone (#20)
* add attribute for task reference * fix pre-commit and bump version --------- Co-authored-by: Arrowana <[email protected]>
1 parent 7410bfd commit 3a5cea9

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

example_publisher/provider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ class Price:
1313

1414

1515
class Provider(ABC):
16+
_update_loop_task = None
17+
1618
@abstractmethod
1719
def upd_products(self, product_symbols: List[Symbol]):
1820
...
1921

2022
def start(self) -> None:
21-
asyncio.create_task(self._update_loop())
23+
self._update_loop_task = asyncio.create_task(self._update_loop())
2224

2325
@abstractmethod
2426
async def _update_loop(self):

example_publisher/providers/pyth_replicator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, config: PythReplicatorConfig) -> None:
3131
self._prices: Dict[
3232
str, Tuple[float | None, float | None, UnixTimestamp | None]
3333
] = {}
34+
self._update_accounts_task: asyncio.Task | None = None
3435

3536
async def _update_loop(self) -> None:
3637
self._ws = self._client.create_watch_session()
@@ -41,7 +42,7 @@ async def _update_loop(self) -> None:
4142
self._config.program_key, await self._client.get_all_accounts()
4243
)
4344

44-
asyncio.create_task(self._update_accounts_loop())
45+
self._update_accounts_task = asyncio.create_task(self._update_accounts_loop())
4546

4647
while True:
4748
update = await self._ws.next_update()

example_publisher/publisher.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Product:
2727
class Publisher:
2828
def __init__(self, config: Config) -> None:
2929
self.config: Config = config
30+
self._product_update_task: asyncio.Task | None = None
3031

3132
if not getattr(self.config, self.config.provider_engine):
3233
raise ValueError(f"Missing {self.config.provider_engine} config")
@@ -48,7 +49,9 @@ def __init__(self, config: Config) -> None:
4849
async def start(self):
4950
await self.pythd.connect()
5051

51-
asyncio.create_task(self._start_product_update_loop())
52+
self._product_update_task = asyncio.create_task(
53+
self._start_product_update_loop()
54+
)
5255

5356
async def _start_product_update_loop(self):
5457
await self._upd_products()

example_publisher/pythd.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ 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()
4647

4748
async def connect(self) -> Server:
4849
self.server = Server(self.address)
@@ -69,7 +70,11 @@ async def subscribe_price_sched(self, account: str) -> int:
6970

7071
def _notify_price_sched(self, subscription: int) -> None:
7172
log.debug("notify_price_sched RPC call received", subscription=subscription)
72-
asyncio.get_event_loop().create_task(self.on_notify_price_sched(subscription))
73+
task = asyncio.get_event_loop().create_task(
74+
self.on_notify_price_sched(subscription)
75+
)
76+
self._notify_price_sched_tasks.add(task)
77+
task.add_done_callback(lambda: self._notify_price_sched_tasks.remove(task))
7378

7479
async def all_products(self) -> List[Product]:
7580
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.1"
3+
version = "1.0.2"
44
description = ""
55
authors = []
66
license = "Apache-2"

0 commit comments

Comments
 (0)