Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - Tcl_AsyncDelete: async handler deleted by the wrong thread?

I'm asking this question because I can't solve one problem in Python/Django (actually in pure Python it's ok) which leads to RuntimeError: tcl_asyncdelete async handler deleted by the wrong thread. This is somehow related to the way how I render matplotlib plots in Django. The way I do it is:

... import matplotlib.pyplot as plt ... fig = plt.figure() ... plt.close() 

I extremely minimized my code. But the catch is - even if I have just one line of code:

fig = plt.figure() 

I see this RuntimeError happening. I hope I could solve the problem, If I knew the correct way of closing/cleaning/destroying plots in Python/Django.

like image 689
Jacobian Avatar asked Nov 26 '14 10:11

Jacobian


People also ask

What does TCL_asyncdelete mean?

python - Tcl_AsyncDelete: async handler deleted by the wrong thread, but I didn't delete from wrong thread? - Stack Overflow Tcl_AsyncDelete: async handler deleted by the wrong thread, but I didn't delete from wrong thread? My python program allows users to make a maze by drag and drop interference.

How to run the Tkinter Matplotlib backend in anything other than mainloop?

But the problem seems clear: you can't run the tkinter matplotlib backend in anything other than the main loop. And I doubt that you can use the Kivy mainloop with the Tk libraries. You've got to use Tk.mainloop (). Better to try the matplotlib backend from the Kivy Garden.

How do you handle multiple threads in Tkinter?

When using threads in tkinter applications, it's a good idea to keep all of the tkinter stuff in one thread. If you split any tkinter widgets or operations into separate threads, it can cause problems. You should put the for loop inside load (), and put everything that's currently inside load () where the for loop was.


1 Answers

By default matplotlib uses TK gui toolkit, when you're rendering an image without using the toolkit (i.e. into a file or a string), matplotlib still instantiates a window that doesn't get displayed, causing all kinds of problems. In order to avoid that, you should use an Agg backend. It can be activated like so --

import matplotlib matplotlib.use('Agg') from matplotlib import pyplot 

For more information please refer to matplotlib documentation -- http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server

like image 52
Alex Volkov Avatar answered Sep 18 '22 20:09

Alex Volkov