1

How to use Qt4Reactor the right way?

I have a PyQt4 Application running Scrapy to parse a website and show the results in the ui.

When the user clicks a button, the scraper gets started in another process. The ui blocks and the scraper is running. When the scraper is finished, the data is shown in the ui. What I want is a non-blockung ui when Scrapy is running.

Since Scrapy is build on top of twisted, I have to use a new process instead of a thread to start it from the ui.

My question is how to achieve a non-blocking ui as simple as possible?

I tried to use Qt4Reactor like this:

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)

    import qt4reactor
    qt4reactor.install()

    MainWindow = DiscogsRenamer()
    MainWindow.show()

    sys.exit(app.exec_())
    reactor.runReturn()

This results in an error:

Traceback (most recent call last):
    File "/home/f/work/py/discogs_renamer/main.py", line 224, in <module>
        qt4reactor.install()
    File "/usr/local/lib/python2.7/dist-packages/qt4reactor.py", line 338, in posixinstall
        installReactor(p)
    File "/usr/local/lib/python2.7/dist-packages/twisted/internet/main.py", line 32, in installReactor
        raise error.ReactorAlreadyInstalledError("reactor already installed")
twisted.internet.error.ReactorAlreadyInstalledError: reactor already installed

I posted another question to this topic: Integrate Scrapy/Twisted event loop in PyQt4 Main loop

Thanks in advance.

Community
  • 1
  • 1
user937284
  • 2,454
  • 6
  • 25
  • 29

1 Answers1

0

You must import and install() the qt4reactor before importing twisted.internet.reactor.

import qt4reactor
qt4reactor.install()
from twisted.internet import reactor
amsh
  • 210
  • 6
  • 15