Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 32 additions & 35 deletions 67 src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
Copy link
Contributor Author

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

repeats: !single
block: ^(NSTimer *timer) {
dispatch_async(dispatch_get_main_queue(), ^{
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Expand All @@ -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",
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.