[英]multithreading matplotlib graph in a kivy app
I wish to generate several graphs by using matplotlib when i'm leaving my kivy app. 我希望在离开我的kivy应用程序时使用matplotlib生成几个图形。
Problem 问题
Generate 4-6 graphs in .png takes time (around 1 or 2 minutes in my case), that's why I wished to multithread my process but it didn't work (got the exceptionError below...) 在.png中生成4-6张图形需要花费时间(在我的情况下,大约需要1或2分钟),这就是为什么我希望对进程进行多线程处理但无法正常工作(在下面得到了exceptionError ...)
[WARNING] [generer graph] chemin : /media/pi/DATA8/2019-3-15-10-40-26/data_dut_1_2019-3-15-10-40-26.csv
[WARNING] [generer_graph] nom_graph : /media/pi/DATA8/2019-3-15-10-40-26/data_dut_1_2019-3-15-10-40-26.png
[WARNING] [generer graph] chemin : /media/pi/DATA8/2019-3-15-10-40-26/data_dut_2_2019-3-15-10-40-26.csv
[WARNING] [generer graph] chemin : /media/pi/DATA8/2019-3-15-10-40-26/data_dut_3_2019-3-15-10-40-26.csv
[WARNING] [generer_graph] nom_graph : /media/pi/DATA8/2019-3-15-10-40-26/data_dut_2_2019-3-15-10-40-26.png
[WARNING] [generer_graph] nom_graph : /media/pi/DATA8/2019-3-15-10-40-26/data_dut_3_2019-3-15-10-40-26.png
[WARNING] [generer graph] chemin : /media/pi/DATA8/2019-3-15-10-40-26/data_dut_4_2019-3-15-10-40-26.csv
[WARNING] [generer_graph] nom_graph : /media/pi/DATA8/2019-3-15-10-40-26/data_dut_4_2019-3-15-10-40-26.png
Exception in thread Thread-4:
Traceback (most recent call last):
Exception in thread Thread-3:
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
Traceback (most recent call last):
self.run()
Exception in thread Thread-6:
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
File "interfacebancdetest_dev/main.py", line 217, in run
self.run()
Traceback (most recent call last):
genGraph.generer_graph(chemin=self.chemin, y_low_min=self.y_low_min, y_low_max=self.y_low_max, y_high_min=self.y_high_min, y_high_max=self.y_high_max, format_graph=self.format_graph)
File "interfacebancdetest_dev/main.py", line 217, in run
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
File "/home/pi/Desktop/interfacebancdetest_dev/genGraph.py", line 48, in generer_graph
genGraph.generer_graph(chemin=self.chemin, y_low_min=self.y_low_min, y_low_max=self.y_low_max, y_high_min=self.y_high_min, y_high_max=self.y_high_max, format_graph=self.format_graph)
self.run()
plt.plot(abcisse, ordonnee, marker=',')
File "/home/pi/Desktop/interfacebancdetest_dev/genGraph.py", line 48, in generer_graph
File "interfacebancdetest_dev/main.py", line 217, in run
plt.plot(abcisse, ordonnee, marker=',')
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3092, in plot
genGraph.generer_graph(chemin=self.chemin, y_low_min=self.y_low_min, y_low_max=self.y_low_max, y_high_min=self.y_high_min, y_high_max=self.y_high_max, format_graph=self.format_graph)
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3092, in plot
ax = gca()
ax = gca()
File "/home/pi/Desktop/interfacebancdetest_dev/genGraph.py", line 48, in generer_graph
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 828, in gca
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 828, in gca
ax = gcf().gca(**kwargs)
plt.plot(abcisse, ordonnee, marker=',')
ax = gcf().gca(**kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1268, in gca
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3092, in plot
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1268, in gca
return self.add_subplot(1, 1, 1, **kwargs)
ax = gca()
return self.add_subplot(1, 1, 1, **kwargs)
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 960, in add_subplot
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 828, in gca
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 960, in add_subplot
self._axstack.add(key, a)
ax = gcf().gca(**kwargs)
self._axstack.add(key, a)
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 124, in add
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1268, in gca
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 124, in add
Stack.remove(self, (key, a_existing))
return self.add_subplot(1, 1, 1, **kwargs)
Stack.remove(self, (key, a_existing))
File "/usr/lib/python2.7/dist-packages/matplotlib/cbook.py", line 1383, in remove
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 960, in add_subplot
File "/usr/lib/python2.7/dist-packages/matplotlib/cbook.py", line 1383, in remove
raise ValueError('Unknown element o')
self._axstack.add(key, a)
raise ValueError('Unknown element o')
ValueError: Unknown element o
File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 124, in add
ValueError: Unknown element o
Stack.remove(self, (key, a_existing))
File "/usr/lib/python2.7/dist-packages/matplotlib/cbook.py", line 1383, in remove
raise ValueError('Unknown element o')
ValueError: Unknown element o
[INFO ] [Base ] Leaving application in progress...
As I understand it, it could be a problem of shared intern ressources between the differents threads, but I relly don't know how to solve this one.... 据我了解,它可能是不同线程之间共享实习生资源的问题,但我不知道如何解决这个问题....
My code : 我的代码:
if(passerelle.graph1.flagGraph):
t1=Thread_graph(chemin=chemin1, y_low_min=passerelle.graph1.y_low_min, y_low_max=passerelle.graph1.y_low_max, y_high_min=passerelle.graph1.y_high_min, y_high_max=passerelle.graph1.y_high_max, format_graph=passerelle.graph1.setTaille)
t2=Thread_graph(chemin=chemin2, y_low_min=passerelle.graph1.y_low_min, y_low_max=passerelle.graph1.y_low_max, y_high_min=passerelle.graph1.y_high_min, y_high_max=passerelle.graph1.y_high_max, format_graph=passerelle.graph1.setTaille)
t3=Thread_graph(chemin=chemin3, y_low_min=passerelle.graph1.y_low_min, y_low_max=passerelle.graph1.y_low_max, y_high_min=passerelle.graph1.y_high_min, y_high_max=passerelle.graph1.y_high_max, format_graph=passerelle.graph1.setTaille)
t4=Thread_graph(chemin=chemin4, y_low_min=passerelle.graph1.y_low_min, y_low_max=passerelle.graph1.y_low_max, y_high_min=passerelle.graph1.y_high_min, y_high_max=passerelle.graph1.y_high_max, format_graph=passerelle.graph1.setTaille)
try:
t1.start()
#genGraph.generer_graph(chemin=chemin1, y_low_min=passerelle.graph1.y_low_min, y_low_max=passerelle.graph1.y_low_max, y_high_min=passerelle.graph1.y_high_min, y_high_max=passerelle.graph1.y_high_max, format_graph=passerelle.graph1.setTaille)
except AttributeError:
Logger.warning('fermerBanc: impossible de generer un graph 1, fichier vide')
try:
t2.start()
#genGraph.generer_graph(chemin=chemin2, y_low_min=passerelle.graph1.y_low_min, y_low_max=passerelle.graph1.y_low_max, y_high_min=passerelle.graph1.y_high_min, y_high_max=passerelle.graph1.y_high_max, format_graph=passerelle.graph1.setTaille)
except AttributeError:
Logger.warning('fermerBanc: impossible de generer un graph 2, fichier vide')
try:
t3.start()
#genGraph.generer_graph(chemin=chemin3, y_low_min=passerelle.graph1.y_low_min, y_low_max=passerelle.graph1.y_low_max, y_high_min=passerelle.graph1.y_high_min, y_high_max=passerelle.graph1.y_high_max, format_graph=passerelle.graph1.setTaille)
except AttributeError:
Logger.warning('fermerBanc: impossible de generer un graph 3, fichier vide')
try:
t4.start()
#genGraph.generer_graph(chemin=chemin4, y_low_min=passerelle.graph1.y_low_min, y_low_max=passerelle.graph1.y_low_max, y_high_min=passerelle.graph1.y_high_min, y_high_max=passerelle.graph1.y_high_max, format_graph=passerelle.graph1.setTaille)
except AttributeError:
Logger.warning('fermerBanc: impossible de generer un graph 4, fichier vide')
t1.join()
t2.join()
t3.join()
t4.join()
My class 我的课
class Thread_graph(Thread):
"""Permet le multi threading de generation de graph"""
def __init__(self, chemin, y_low_min, y_low_max, y_high_min, y_high_max, format_graph):
Thread.__init__(self)
self.chemin = chemin
self.y_low_min=y_low_min
self.y_low_max=y_low_max
self.y_high_min=y_high_min
self.y_high_max=y_high_max
self.format_graph=format_graph
def run(self):
"""code à executer pendand l'executiond u thread"""
genGraph.generer_graph(chemin=self.chemin, y_low_min=self.y_low_min, y_low_max=self.y_low_max, y_high_min=self.y_high_min, y_high_max=self.y_high_max, format_graph=self.format_graph)
Found that due to back end conditions, matplotlib is not "thread safe". 发现由于后端条件,matplotlib不是“线程安全”。 So i used process instead (And since my fonction needed several arguments, I had to modify it to pass a list of objects instead)
所以我使用了进程代替(因为我的函数需要几个参数,所以我必须修改它来传递一个对象列表)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.