Partage
  • Partager sur Facebook
  • Partager sur Twitter

Multithreading PyQt5 python

erreur incompréhensible

    26 juillet 2019 à 13:27:05

    Bonjour,

    j'essaie de faire du scraping avec PyQt en parallèle grâce au multithreading mais j'obtiens une erreur que je ne comprends pas.

    Voici le code : 

    from bs4 import BeautifulSoup
    import sys
    import requests
    from PyQt5.QtWebEngineWidgets import QWebEnginePage
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtCore import QUrl
    from threading import Thread
    
    url = "https://www.booking.com/searchresults.fr.html?label=gen173nr-1FCAEoggI46AdIM1gEaE2IAQGYAQ24ARjIAQzYAQHoAQH4AQuIAgGoAgS4Ar3x5ukFwAIB&sid=a454e37209591e054b02e8917d61befe&tmpl=searchresults&checkin_month=8&checkin_monthday=1&checkin_year=2019&checkout_month=8&checkout_monthday=2&checkout_year=2019&class_interval=1&dest_id=-1456928&dest_type=city&dtdisc=0&from_sf=1&group_adults=1&group_children=0&inac=0&index_postcard=0&label_click=undef&no_rooms=1&postcard=0&raw_dest_type=city&room1=A&sb_price_type=total&shw_aparth=1&slp_r_match=0&src_elem=sb&srpvid=14466c78e41800bd&ss=Paris&ss_all=0&ssb=empty&sshis=0&ssne=Paris&ssne_untouched=Paris&order=review_score_and_price"
    
    
    class Page(QWebEnginePage):
        def __init__(self, url):
            #self.app = QApplication(sys.argv)
            QWebEnginePage.__init__(self)
            self.html = ''
            self.loadFinished.connect(self._on_load_finished)
            self.load(QUrl(url))
            #self.app.exec_()
    
        def _on_load_finished(self):
            self.html = self.toHtml(self.Callable)
            print('Load finished')
    
        def Callable(self, html_str):
            self.html = html_str
            self.app.quit()
    
    
    class test(Thread):
        def __init__(self, number):
            Thread.__init__(self)
            self.number = number
            print(self.number)
    
        def run(self):
            """Code à exécuter pendant l'exécution du thread."""
            page = Page(url)
            soup = BeautifulSoup(page.html, 'html.parser')
            print(self.number)
            f = open("test" + self.number + ".html", "w")
            f.write(soup)
            f.close()
    
    app = QApplication(sys.argv)
    
    thread1 = test("1")
    thread2 = test("2")
    
    thread1.start()
    thread2.start()
    
    thread1.join()
    thread2.join()
    
    app.exec_()
    

    et voici le message d'erreur :

    QObject::startTimer: Timers can only be used with threads started with QThread

    QObject::startTimer: Timers can only be used with threads started with QThread

    QBasicTimer::start: QBasicTimer can only be used with threads started with QThread

    QObject::startTimer: Timers can only be used with threads started with QThread

    Segmentation fault: 11


    Quelqu'un a une idée ?

    • Partager sur Facebook
    • Partager sur Twitter
      26 juillet 2019 à 22:48:52

      Je ne suis pas connaisseur de Qt, mais il semble qu'il utilise les Qthread justement pour faire le threading, alors que tu utilises le module threading standard de Python. Ca fait des problèmes du coté du code C avec un joli segfault visiblement.

      Donc privilégier le Qthread je pense que ça résoudra ton problème. J'ai rien trouvé sur la docu de PyQt du peu que j'ai cherché, mais j'ai quand meme trouvé quelques liens si jamais :
       - PyQt threading tutorial

       - Qthread sur docu de Qt

      - Une musique à écouter

      -
      Edité par AbcSxyZ 26 juillet 2019 à 22:57:09

      • Partager sur Facebook
      • Partager sur Twitter
      Pour réussir dans la vie, je te conseille de te piquer à l'héro jusqu'à en crever.

      Multithreading PyQt5 python

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown