1

I have a program mainly composed of a QMainWindow. But I added some kind of "plugins", which do some particular things. One of them for example parses thepiratebay to return the latest torrents of a category and put them in a database:

def parse(self):
    bdd = sqlite3.connect("fichiers.sqlite")
    bdd.row_factory = sqlite3.Row

    c = bdd.cursor() # obtention d'un curseur
    c.execute("SELECT * FROM pirate ORDER BY id ASC LIMIT 1")

    try:
        last_title = c.fetchone()['title']
        print("Dernier titre: " + last_title)
    except TypeError:
        last_title = ""

    search = self.t.search('*', category=CATEGORIES.VIDEOS.MOVIES)

    go = True
    j = 0
    for i in range(0, 34):
        print(i)
        search.order(ORDERS.UPLOADED.DES).page(i)

        for torrent in search:
            if torrent.title == last_title:
                print("on sort")
                go = False
                break

            print(torrent.title)
            j += 1
            c.execute("INSERT INTO pirate(title, title_simple, user, magnet_link, \
                       url, created, size) VALUES (?, ?, ?, ?, ?, ?, ?)", \
                       (torrent.title, simpleChar(torrent.title), torrent.user, torrent.magnet_link,
                        str(torrent.url), torrent.created, strByteToOctet(torrent.size)[1]))

        if not go:
            break

    print(j)
    bdd.commit()
    c.close()
    bdd.close()

    self.modele.setTable("pirate")
    self.modele.select()

But when I do that in my Qt program, the rest of the program waits until the function finishes the parsing. The API I use sends requests to the website, so it is sometimes a bit long. So the question is:

How do I parse without waiting the end of this function? I would like the Qt program to start that in a thread, without blocking the rest.

László Papp
  • 51,870
  • 39
  • 111
  • 135
JPFrancoia
  • 4,866
  • 10
  • 43
  • 73
  • 1
    Using threads is convenient in Qt. You should read a tutorial for your library (pyqt or pyside) and try to write something on your own. If you meet any specific problem, you should edit the question adding details. – Pavel Strakhov Dec 23 '13 at 11:35
  • Found it: http://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt. I couldn't guess it was so easy. I just had to make my class inherit from QThread and rename my function parse into run, and that's it. – JPFrancoia Dec 23 '13 at 19:21

1 Answers1

0

As you noted yourself, using a custom thread based on QThread requires two things initially:

1) Inheriting from QThread.

2) You need to reimplement the virtual protected run method that the start method will call based on the template method pattern.

That is pretty much it for starter, and then you can start your thread. Once, you go further, you may need to deal with thread pools and so forth, but that is for later.strong text

László Papp
  • 51,870
  • 39
  • 111
  • 135