-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
FIX/ENH: macos: dispatch timer tasks asynchronously to the main loop #29030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1729,6 +1729,29 @@ - (void)flagsChanged:(NSEvent *)event | |
(void*) self, (void*)(self->timer)); | ||
} | ||
|
||
static void | ||
Timer__timer_stop_impl(Timer* self) | ||
{ | ||
if (self->timer) { | ||
[self->timer invalidate]; | ||
self->timer = NULL; | ||
} | ||
} | ||
|
||
static PyObject* | ||
Timer__timer_stop(Timer* self) | ||
{ | ||
Timer__timer_stop_impl(self); | ||
Py_RETURN_NONE; | ||
} | ||
|
||
static void | ||
Timer_dealloc(Timer* self) | ||
{ | ||
Timer__timer_stop_impl(self); | ||
Py_TYPE(self)->tp_free((PyObject*)self); | ||
} | ||
|
||
static PyObject* | ||
Timer__timer_start(Timer* self, PyObject* args) | ||
{ | ||
|
@@ -1748,19 +1771,16 @@ - (void)flagsChanged:(NSEvent *)event | |
} | ||
|
||
// hold a reference to the timer so we can invalidate/stop it later | ||
self->timer = [NSTimer timerWithTimeInterval: interval | ||
repeats: !single | ||
block: ^(NSTimer *timer) { | ||
gil_call_method((PyObject*)self, "_on_timer"); | ||
if (single) { | ||
// A single-shot timer will be automatically invalidated when it fires, so | ||
// we shouldn't do it ourselves when the object is deleted. | ||
self->timer = NULL; | ||
} | ||
self->timer = [NSTimer scheduledTimerWithTimeInterval: interval | ||
repeats: !single | ||
block: ^(NSTimer *timer) { | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the major change here. We are dispatching our event asynchronously (so as to not sequentially depend on the callback processing time), but putting it onto the main thread's queue. |
||
gil_call_method((PyObject*)self, "_on_timer"); | ||
if (single) { | ||
Timer__timer_stop_impl(self); | ||
} | ||
}); | ||
}]; | ||
// Schedule the timer on the main run loop which is needed | ||
// when updating the UI from a background thread | ||
[[NSRunLoop mainRunLoop] addTimer: self->timer forMode: NSRunLoopCommonModes]; | ||
|
||
exit: | ||
Py_XDECREF(py_interval); | ||
|
@@ -1773,29 +1793,6 @@ - (void)flagsChanged:(NSEvent *)event | |
} | ||
} | ||
|
||
static void | ||
Timer__timer_stop_impl(Timer* self) | ||
{ | ||
if (self->timer) { | ||
[self->timer invalidate]; | ||
self->timer = NULL; | ||
} | ||
} | ||
|
||
static PyObject* | ||
Timer__timer_stop(Timer* self) | ||
{ | ||
Timer__timer_stop_impl(self); | ||
Py_RETURN_NONE; | ||
} | ||
|
||
static void | ||
Timer_dealloc(Timer* self) | ||
{ | ||
Timer__timer_stop_impl(self); | ||
Py_TYPE(self)->tp_free((PyObject*)self); | ||
} | ||
|
||
static PyTypeObject TimerType = { | ||
PyVarObject_HEAD_INIT(NULL, 0) | ||
.tp_name = "matplotlib.backends._macosx.Timer", | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This automatically schedules the timer on the main runloop for us.
https://developer.apple.com/documentation/foundation/timer/2091889-scheduledtimer