0

I am having trouble using multiprocessing with pyqt as it opens up multiple windows and doesn't really exceute the target function. I created my interface in Qt Designer and my example code is as follows:

from multiprocessing import Pool
from PyQt5 import uic, QtWidgets
from PyQt5.QtWidgets import *
import sys

def updater(num):
    print(num)

def main_tracker():
    p = Pool(processes=4)
    data = p.map(updater, range(0, 100))

app=QtWidgets.QApplication(sys.argv)
window = uic.loadUi("tracker.ui")
window.pushButton.clicked.connect(main_tracker)
window.show()
sys.exit(app.exec_())

On running this, the interface opens as normal, but when I click on the pushbutton on the gui it simply opens multiple pyqt windows and doesnt run the functions as expected. How can I get this working so that the multiprocessing works and without opening multiple windows? I have seen kind of similar questions on here but I haven't found one that has a solution addressing my problem.

The code for the ui file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>365</width>
    <height>134</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>69</x>
      <y>19</y>
      <width>173</width>
      <height>59</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>365</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
5
  • I created my own tracker.ui for obvious reasons and I don't see any windows open, besides multiprocessing works correctly, you can provide the .ui Commented Jul 24, 2019 at 14:39
  • @eyllanesc Sure I have edited my question and added the generated code for the ui file Commented Jul 24, 2019 at 14:47
  • For me it works correctly, I recommend you run it in the CMD/Terminal to rule out that the problem is the IDE. Commented Jul 24, 2019 at 14:49
  • @eyllanesc It still does the same thing and just opens multiple windows without printing anything. My file is named testfile.py and I'm running it on cmd as python testfile.py. Same issue even just double clicking the file. Before I was running it in VS Code. Commented Jul 24, 2019 at 14:56
  • 1
    Check the safe importing of main module requirements. Commented Jul 24, 2019 at 16:30

1 Answer 1

4

I have two remarks. Always use

if __name__ == "__main__":
   code here

construction for main file. in your case it will be:

from multiprocessing import Pool
from PyQt5 import uic, QtWidgets
from PyQt5.QtWidgets import *
import sys

def updater(num):
    print(num)

def main_tracker():
    p = Pool(processes=4)
    data = p.map(updater, range(0, 100))

if __name__ == "__main__":
    app=QtWidgets.QApplication(sys.argv)
    window = uic.loadUi("tracker.ui")
    window.pushButton.clicked.connect(main_tracker)
    window.show()
    sys.exit(app.exec_())

and if you would like to freeze your application (create exec file) on windows then add this lines to your code (or maybe it will fix your error)

import multiprocessing

multiprocessing.freeze_support()

otherwise you will meet again the problem with multiple window

Sign up to request clarification or add additional context in comments.

2 Comments

Do you reckon its possible for this case to have the gui running as a separate thread? I'm quite new to threading and all my attempts have been futile
The Qt gui need to be runed in main thread. But you can delegate some calculations to worker thread. You can use python thread for classical case or QThread for calculations which will support signals. doc.qt.io/qt-5/signalsandslots.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.