IdentifiantMot de passe
Loading...
Mot de passe oubli� ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les r�ponses en temps r�el, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Generer des graphes avec matplotlib en multithreading


Sujet :

Python

  1. #1
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    Octobre 2017
    Messages
    53
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (�le de France)

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Octobre 2017
    Messages : 53
    Par d�faut Generer des graphes avec matplotlib en multithreading
    Bonjour � tous.

    Dans le cadre d'une application que je d�veloppe avec Kivy, je dois � la fermeture de celle-ci g�n�rer plusieurs graph (entre 4 et 6) avec matplotlib. Probl�me : chaque graph peut avoir plusieurs dizaines voire centaines de milliers de points. J'essaye donc de les g�nerer en parrall�le gr�ce au mutlthreading.
    Ce que j'ai fait jusqu'� maintenant :

    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    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)
    Ex�cution :

    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     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()
    Ma fonction "generer_graph" genere un graph en .png.

    Le probl�me c'est qu'� l'appel de ce code, j'obtiens l'erreur suivante :

    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    [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...
    Je pense qu'il s'agit d'un probl�me de ressources partag�, mais je ne saurais pas en dire plus...

    Merci d'avance pour votre aide !

  2. #2
    Expert �minent
    Homme Profil pro
    Architecte technique retrait�
    Inscrit en
    Juin 2008
    Messages
    21 772
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activit� : Architecte technique retrait�
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 772
    Par d�faut
    Salut,

    Citation Envoy� par StanSmooth Voir le message
    Je pense qu'il s'agit d'un probl�me de ressources partag�, mais je ne saurais pas en dire plus...
    Un peu de recherche sur Internet vous indiquerait que matplotlib n'est pas "thread-safe" parce que les backends (les biblioth�ques graphiques) ne sont pas thread-safe.
    A partir de l�, il va falloir trouver autre chose... et l� encore, un peu de recherche sur Internet pour savoir ce qu'ont fait ceux qui ont �t� confront�s � ce genre de pbs et voir si vous pouvez int�grer ces solutions...

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  3. #3
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    Octobre 2017
    Messages
    53
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (�le de France)

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Octobre 2017
    Messages : 53
    Par d�faut
    Bonjour Wiztricks,
    Merci pour votre r�ponse constructive.
    Les recherches que j'avaient effectu� m'ont conduit � l'utilisation de multiprocess. Malheureusement, c'est un proc�d�, que je connais mal (notamment pour pouvoir faire passer plusieurs arguments dans un process), et j'aurais aim� pouvoir utiliser du multithreading. Apr�s si c'est impossible, je vais me rabattre sur le multiprocessing.

  4. #4
    Expert confirm�
    Avatar de tyrtamos
    Homme Profil pro
    Retrait�
    Inscrit en
    D�cembre 2007
    Messages
    4 486
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes C�te d'Azur)

    Informations professionnelles :
    Activit� : Retrait�

    Informations forums :
    Inscription : D�cembre 2007
    Messages : 4 486
    Billets dans le blog
    6
    Par d�faut
    Bonjour,

    A partir de Python 2.6, le module "multiprocessing" a �t� rendu proche dans sa syntaxe du multithreading, ce qui facilite grandement son utilisation. Et il a l'avantage de permettre l'utilisation simultan�e des c�urs des CPU actuels, contrairement au multithreading.

    => https://docs.python.org/2/library/mu...ultiprocessing

    Pour la question pos�e, il faudrait garder tout ce qui est graphique dans la partie principale du programme, et faire les calculs longs en multiprocessing.

  5. #5
    Expert �minent
    Homme Profil pro
    Architecte technique retrait�
    Inscrit en
    Juin 2008
    Messages
    21 772
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activit� : Architecte technique retrait�
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 772
    Par d�faut
    Citation Envoy� par StanSmooth Voir le message
    Malheureusement, c'est un proc�d�, que je connais mal (notamment pour pouvoir faire passer plusieurs arguments dans un process), et j'aurais aim� pouvoir utiliser du multithreading. Apr�s si c'est impossible, je vais me rabattre sur le multiprocessing.
    De toutes fa�ons, plusieurs threads en Python ne vous permettront pas d'utiliser plus de ressources que celles d'un seul CPU. Pour aller plus vite dans des calculs, vous n'avez pas trop le choix.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  6. #6
    Membre averti
    Homme Profil pro
    �tudiant
    Inscrit en
    Octobre 2017
    Messages
    53
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (�le de France)

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Octobre 2017
    Messages : 53
    Par d�faut
    J'ai finalement opt� pour du multiprocessing, et les resultats semblent s'�tre am�lior�, merci � tous

Discussions similaires

  1. Generer des graphes avec GWT
    Par nanson dans le forum GWT et Vaadin
    R�ponses: 4
    Dernier message: 15/07/2009, 10h52
  2. Generer des graphes � partir de donn�es contenue dans un serveur
    Par Premium dans le forum API standards et tierces
    R�ponses: 1
    Dernier message: 09/05/2007, 17h31
  3. Probl�me d'affichage des graphes avec Tomcat
    Par _Janu_ dans le forum BIRT
    R�ponses: 11
    Dernier message: 20/09/2006, 15h30
  4. Generer des pdf avec Birt
    Par soumou dans le forum BIRT
    R�ponses: 1
    Dernier message: 07/09/2006, 09h56
  5. tracer des graphes avec php
    Par estampille dans le forum Langage
    R�ponses: 1
    Dernier message: 19/07/2006, 18h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo