Skip to content

Commit 090d117

Browse files
authored
Merge pull request #9 from ezmsg-org/cboulay/async_reset_state
Add async `_areset_state` to stateful processor and producer.
2 parents 0e23305 + a325762 commit 090d117

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

‎src/ezmsg/baseproc/stateful.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ def _reset_state(self, message: typing.Any) -> None:
118118
"""
119119
...
120120

121+
async def _areset_state(self, message: typing.Any) -> None:
122+
"""
123+
Async variant of `_reset_state`. Override this if reset requires async work;
124+
in that case `_reset_state` should bridge via `run_coroutine_sync(self._areset_state(message))`.
125+
"""
126+
return self._reset_state(message)
127+
121128
@abstractmethod
122129
def _process(self, message: typing.Any) -> typing.Any: ...
123130

@@ -131,7 +138,7 @@ def __call__(self, message: typing.Any) -> typing.Any:
131138
async def __acall__(self, message: typing.Any) -> typing.Any:
132139
msg_hash = self._hash_message(message)
133140
if msg_hash != self._hash:
134-
self._reset_state(message)
141+
await self._areset_state(message)
135142
self._hash = msg_hash
136143
return await self._aprocess(message)
137144

@@ -179,9 +186,16 @@ def _reset_state(self) -> None:
179186
"""
180187
...
181188

189+
async def _areset_state(self) -> None:
190+
"""
191+
Async variant of `_reset_state`. Override this if reset requires async work;
192+
in that case `_reset_state` should bridge via `run_coroutine_sync(self._areset_state())`.
193+
"""
194+
return self._reset_state()
195+
182196
async def __acall__(self) -> MessageOutType:
183197
if self._hash == -1:
184-
self._reset_state()
198+
await self._areset_state()
185199
self._hash = 0
186200
return await self._produce()
187201

@@ -319,7 +333,7 @@ async def apartial_fit_transform(self, message: AxisArray) -> MessageOutType:
319333
"""Async variant of partial_fit_transform."""
320334
msg_hash = self._hash_message(message)
321335
if msg_hash != self._hash:
322-
self._reset_state(message)
336+
await self._areset_state(message)
323337
self._hash = msg_hash
324338
await self.apartial_fit(message)
325339
return await self._aprocess(message)
@@ -351,6 +365,6 @@ async def __acall__(self, message: MessageInType) -> MessageOutType:
351365
# Earlier versions must be explicit: `await obj.__acall__(message)`
352366
msg_hash = self._hash_message(message)
353367
if msg_hash != self._hash:
354-
self._reset_state(message)
368+
await self._areset_state(message)
355369
self._hash = msg_hash
356370
return await self._aprocess(message)

0 commit comments

Comments
 (0)