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

Conversation

greglucas
Copy link
Contributor

PR summary

Previously, the timers were dependent on the length of time it took for the timer callback to execute. This dispatches the callback to the main thread's task queue to avoid synchronously waiting on long-running callback tasks.

Note that I didn't add any tests here because there is inconsistency between backends, so other backends fail for these assertions currently. I've opened up an issue to discuss that: #29029

On main, this script prints every 2 seconds callback time + timer interval, with this PR it prints every second max(callback time, timer interval).

import time
import matplotlib.pyplot as plt

fig = plt.figure()

def on_timer():
    print(time.ctime())
    time.sleep(1)

timer = fig.canvas.new_timer(interval=1000)
timer.add_callback(on_timer)
timer.start()
fig.canvas.start_event_loop(10)

PR checklist

Previously, the timers were dependent on the length of time it took for
the timer callback to execute. This dispatches the callback to
the task queue to avoid synchronously waiting on long-running callback
tasks.
// 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

self->timer = [NSTimer scheduledTimerWithTimeInterval: interval
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.

@greglucas
Copy link
Contributor Author

closing in favor of doing this all in a consolidated PR: #29062

@greglucas greglucas closed this Nov 1, 2024
@greglucas greglucas deleted the macos-timer-async branch November 1, 2024 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.