Skip to content

Commit

Permalink
Fix wxscheduler issues
Browse files Browse the repository at this point in the history
* Could not schedule actions from other threads
* Slow `schedule` method
  • Loading branch information
christiansandberg committed Mar 6, 2022
1 parent f35ee16 commit c07e746
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions reactivex/scheduler/mainloop/wxscheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down

0 comments on commit c07e746

Please sign in to comment.