Skip to content
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

FIX: Add macos timers to the main thread #27527

Merged
merged 1 commit into from Dec 19, 2023

Conversation

greglucas
Copy link
Contributor

PR summary

The macos timers need to be explicitly added to the main runloop so the drawing takes place there. This causes issues if updating data from other threads and wanting the plot to update.

Fixed previously in: #25553
Which then had a regression via: #26022 which apparently uses a method that puts the timer onto the currently active thread rather than the main one. So, we need to explicitly add the timer to the main runloop.

I tried adding this as an interactive test, but couldn't get it to trigger the draws properly from the subprocess (not sure if it is because we need to set something up differently when running as python -i from a subprocess?). Any thoughts folks have for how to test this so we don't regress again would be welcome.

Same example to manually test with. Save to file, and run with python -i testfile.py. I get a segfault currently trying to draw from a separate thread, and this updates as expected now.

import random
import time
import threading
import matplotlib as mpl
import matplotlib.pyplot as plt


class Plot:

    def __init__(self):
        plt.ion()
        self.start = time.time()
        self.xdata = []
        self.ydata = []
        self.running = None
        fig = plt.figure()
        mpl.rcParams["figure.raise_window"] = True
        plt.show()
        self.axis = fig.add_subplot(111)
        self.line, = self.axis.plot([])
        threading.Thread(target=self.worker).start()

    def worker(self):
        for _ in range(50):
            self.xdata.append(len(self.xdata))
            self.ydata.append(random.random())
            self.axis.set_xlim(0, len(self.xdata))
            self.axis.set_ylim(0, max(self.ydata))
            self.line.set_data(self.xdata, self.ydata)
            time.sleep(0.1)
        print(time.time() - self.start)


if __name__ == '__main__':
    Plot()

PR checklist

The macos timers need to be explicitly added to the main runloop
so the drawing takes place there. This causes issues if updating
data from other threads and wanting the plot to update.
@greglucas greglucas added this to the v3.8.3 milestone Dec 16, 2023
@ksunden ksunden merged commit 45f4888 into matplotlib:main Dec 19, 2023
42 of 43 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Dec 19, 2023
@greglucas greglucas deleted the macos-timer-main branch December 19, 2023 01:05
rcomer added a commit that referenced this pull request Dec 19, 2023
…527-on-v3.8.x

Backport PR #27527 on branch v3.8.x (FIX: Add macos timers to the main thread)
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.

None yet

3 participants