Skip to content

Commit

Permalink
Revisited fix for #372
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogeniola committed May 1, 2024
1 parent 1d3bbca commit 8f1abaf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.7.2b1
0.4.7.2b2
16 changes: 10 additions & 6 deletions meross_iot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,22 +1156,26 @@ def _handle_future(future: Future, result: object, exception: Exception):
future.set_result(result)


async def delayed_execution(coro, delay):
async def delayed_execution(coro, delay, cancel_event):
await asyncio.sleep(delay=delay)
await coro
if not cancel_event.is_set():
await coro


def _schedule_later(coroutine, start_delay, loop):
future = asyncio.run_coroutine_threadsafe(coro=delayed_execution(coro=coroutine, delay=start_delay), loop=loop)
_PENDING_FUTURES.append(DelayedCoroFutureHandler(future, coroutine))
cancel_event = asyncio.Event()
future = asyncio.run_coroutine_threadsafe(coro=delayed_execution(coro=coroutine, delay=start_delay, cancel_event=cancel_event), loop=loop)
_PENDING_FUTURES.append(DelayedCoroFutureHandler(future, coroutine, cancel_event))


class DelayedCoroFutureHandler:
def __init__(self, future, coroutine):
def __init__(self, future, coroutine, cancel_event):
self.future = future
self.coro = coroutine
self.cancel_event = cancel_event

def ensure_canceled(self):
if not self.future.cancelled():
self.future.cancel()
self.coro.close()
self.coro.close()
self.cancel_event.set()

0 comments on commit 8f1abaf

Please sign in to comment.