Python: Qt-Gui and several tasks
<p>I'm trying to use a Qt-Gui and to implement there several task to do work in the background and to update the content in the gui. Here is the code I'm working on (simplified to the minimum). Without gui, ie printing to the terminal, this code works fine:</p>

<pre><code> #!/usr/bin/env python3

import sys
import asyncio
from PyQt4 import QtCore, QtGui, uic

qtCreatorFile = "gui_mini_task.ui"  # Enter file here.

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

@asyncio.coroutine
def task_1(futue_1):
    for i in range(50):
        window.results_window.setText("task_1")
        yield from asyncio.sleep(.1)

@asyncio.coroutine
def task_2(future_2):
    for i in range(1, 10):
        window.results_window.setText("task_2")
        yield from asyncio.sleep(0.5)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()

    future_1 = asyncio.Future()
    future_2 = asyncio.Future()
    tasks = [
        asyncio.async(task_1(future_1)),
        asyncio.async(task_2(future_2))]
    loop = asyncio.get_event_loop()
    window.show() 

    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    sys.exit(app.exec_())
</code></pre>

<p>At the end, ie after 5 seconds, the main window opens and it seems, both task has been executed meanwhile. But my intent is to see the messages in (more or less) real-time, ie the alternance of task_1 and task_2.</p>

<p>Thank you for your precious help!</p>

<p>The folowing is the Code of the "gui_mini_task.ui" in case you want to ...</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;ui version="4.0"&gt;
 &lt;class&gt;MainWindow&lt;/class&gt;
 &lt;widget class="QMainWindow" name="MainWindow"&gt;
  &lt;property name="enabled"&gt;
   &lt;bool&gt;true&lt;/bool&gt;
  &lt;/property&gt;
  &lt;property name="geometry"&gt;
   &lt;rect&gt;
    &lt;x&gt;0&lt;/x&gt;
    &lt;y&gt;0&lt;/y&gt;
    &lt;width&gt;800&lt;/width&gt;
    &lt;height&gt;600&lt;/height&gt;
   &lt;/rect&gt;
  &lt;/property&gt;
  &lt;property name="windowTitle"&gt;
   &lt;string&gt;MainWindow&lt;/string&gt;
  &lt;/property&gt;
  &lt;widget class="QWidget" name="centralwidget"&gt;
   &lt;widget class="QTextEdit" name="results_window"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;40&lt;/x&gt;
      &lt;y&gt;410&lt;/y&gt;
      &lt;width&gt;731&lt;/width&gt;
      &lt;height&gt;71&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QLabel" name="label_3"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;170&lt;/x&gt;
      &lt;y&gt;20&lt;/y&gt;
      &lt;width&gt;161&lt;/width&gt;
      &lt;height&gt;41&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="font"&gt;
     &lt;font&gt;
      &lt;pointsize&gt;20&lt;/pointsize&gt;
      &lt;weight&gt;75&lt;/weight&gt;
      &lt;bold&gt;true&lt;/bold&gt;
     &lt;/font&gt;
    &lt;/property&gt;
    &lt;property name="text"&gt;
     &lt;string&gt;nellcor_gui&lt;/string&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
  &lt;/widget&gt;
  &lt;widget class="QMenuBar" name="menubar"&gt;
   &lt;property name="geometry"&gt;
    &lt;rect&gt;
     &lt;x&gt;0&lt;/x&gt;
     &lt;y&gt;0&lt;/y&gt;
     &lt;width&gt;800&lt;/width&gt;
     &lt;height&gt;27&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
  &lt;/widget&gt;
  &lt;widget class="QStatusBar" name="statusbar"/&gt;
 &lt;/widget&gt;
 &lt;tabstops&gt;
  &lt;tabstop&gt;results_window&lt;/tabstop&gt;
 &lt;/tabstops&gt;
 &lt;resources/&gt;
 &lt;connections/&gt;
&lt;/ui&gt;
</code></pre>

<p>Using quamash like proposed by Andrew Svetlov, the working code looks now like this:</p>

<pre><code>#!/usr/bin/env python3

import sys
import asyncio
from PyQt4 import QtCore, QtGui, uic
from quamash import QEventLoop

qtCreatorFile = "gui_mini_task.ui"  # Enter file here.

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

@asyncio.coroutine
def task_1(futue_1):
    for i in range(50):
        window.results_window.setText("task_1")
        yield from asyncio.sleep(.1)

@asyncio.coroutine
def task_2(future_2):
    for i in range(1, 12):
        window.results_window.setText("task_2")
        yield from asyncio.sleep(0.5)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()
    loop =  QEventLoop(app)
    asyncio.set_event_loop(loop)

    future_1 = asyncio.Future()
    future_2 = asyncio.Future()
    tasks = [
        asyncio.async(task_1(future_1)),
        asyncio.async(task_2(future_2))]

    window.show()  # erst jetzt werden die Widgets instanziert!

    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    sys.exit(app.exec_())
</code></pre>

<p>Thank you!</p>

python qt pyqt multitasking python-asyncio