From c07e746fa4294a1b0d0741acc70104e5eb07a0df Mon Sep 17 00:00:00 2001 From: Christian Sandberg Date: Sun, 20 Feb 2022 20:25:50 +0100 Subject: [PATCH 1/2] Fix wxscheduler issues * Could not schedule actions from other threads * Slow `schedule` method --- reactivex/scheduler/mainloop/wxscheduler.py | 22 +++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/reactivex/scheduler/mainloop/wxscheduler.py b/reactivex/scheduler/mainloop/wxscheduler.py index bcd064b26..a63bf2e52 100644 --- a/reactivex/scheduler/mainloop/wxscheduler.py +++ b/reactivex/scheduler/mainloop/wxscheduler.py @@ -75,9 +75,11 @@ def interval() -> None: log.debug("timeout wx: %s", msecs) timer = self._timer_class(interval) - timer.Start( # type: ignore - msecs, self._wx.TIMER_CONTINUOUS if periodic else self._wx.TIMER_ONE_SHOT - ) + # A timer can only be used from the main thread + if self._wx.IsMainThread(): + timer.Start(msecs, oneShot=not periodic) + else: + self._wx.CallAfter(timer.Start, msecs, oneShot=not periodic) self._timers.add(timer) def dispose() -> None: @@ -99,8 +101,20 @@ def schedule( The disposable object used to cancel the scheduled action (best effort). """ + sad = SingleAssignmentDisposable() + is_disposed = False + + def invoke_action() -> None: + if not is_disposed: + sad.disposable = action(self, state) + + self._wx.CallAfter(invoke_action) - return self._wxtimer_schedule(0.0, action, state=state) + def dispose() -> None: + nonlocal is_disposed + is_disposed = True + + return CompositeDisposable(sad, Disposable(dispose)) def schedule_relative( self, From 25ba53f61fb7744d1327322c28af505805ff3ca6 Mon Sep 17 00:00:00 2001 From: Christian Sandberg Date: Sun, 6 Mar 2022 20:51:08 +0100 Subject: [PATCH 2/2] Silence MyPy warnings --- reactivex/scheduler/mainloop/wxscheduler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reactivex/scheduler/mainloop/wxscheduler.py b/reactivex/scheduler/mainloop/wxscheduler.py index a63bf2e52..2ff631976 100644 --- a/reactivex/scheduler/mainloop/wxscheduler.py +++ b/reactivex/scheduler/mainloop/wxscheduler.py @@ -77,9 +77,9 @@ def interval() -> None: timer = self._timer_class(interval) # A timer can only be used from the main thread if self._wx.IsMainThread(): - timer.Start(msecs, oneShot=not periodic) + timer.Start(msecs, oneShot=not periodic) # type: ignore else: - self._wx.CallAfter(timer.Start, msecs, oneShot=not periodic) + self._wx.CallAfter(timer.Start, msecs, oneShot=not periodic) # type: ignore self._timers.add(timer) def dispose() -> None: