Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ad422a

Browse files
make tkagg blitting threadsafe
current status: Prone to races and hard-to-diagnose timeouts (see python/cpython#21299)
1 parent 0dcdd21 commit 2ad422a

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

lib/matplotlib/backends/_backend_tk.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def __init__(self, figure, master=None, resize_callback=None):
124124
self._tkphoto = tk.PhotoImage(
125125
master=self._tkcanvas, width=w, height=h)
126126
self._findphoto_name = self._tkcanvas.register(self._findphoto)
127+
self._photoputblock_name = self._tkcanvas.register(self._photoputblock)
128+
self._tkpointers = None
127129
self._tkcanvas.create_image(w//2, h//2, image=self._tkphoto)
128130
self._resize_callback = resize_callback
129131
self._tkcanvas.bind("<Configure>", self.resize)
@@ -182,6 +184,10 @@ def resize(self, event):
182184
def _findphoto(self):
183185
return _tkagg.findphoto(self._tkphoto.tk.interpaddr(), str(self._tkphoto))
184186

187+
def _photoputblock(self):
188+
photoptr, dataptr, bboxptr = self._tkpointers
189+
_tkagg.photoputblock(photoptr, dataptr, (0, 1, 2, 3), bboxptr)
190+
185191
def blit(self, bbox=None):
186192
"""
187193
Blit *aggimage* to *photoimage*.
@@ -206,9 +212,9 @@ def blit(self, bbox=None):
206212
else:
207213
self._tkphoto.blank()
208214
bboxptr = (0, width, 0, height)
209-
# photoptr = self._findphoto() # Thread unsafe
210-
photoptr = self._master.tk.call(self._findphoto_name) # Thread safe
211-
_tkagg.photoputblock(photoptr, dataptr, (0, 1, 2, 3), bboxptr) # ???
215+
photoptr = self._master.tk.call(self._findphoto_name)
216+
self._tkpointers = photoptr, dataptr, bboxptr
217+
self._master.tk.call(self._photoputblock_name)
212218

213219
def draw_idle(self):
214220
# docstring inherited

0 commit comments

Comments
 (0)