0

I want to update the GIF while pool process are executing in parallel. Now GIF is getting stuck, it is responding with respect to progressbar value update. I want GIF to execute independently. I have tried using it thread but that is not solution for my process. Once control is going to pool-process it getting GUI freeze, not responding. I have given high level piece of code that in this GIF should not stuck. I am copying number of files through process in the original code there it will take much time to complete the task till then GUI gets not responding. So pool-process I can not remove or replace. Let me know if this GIF continuous movement is possible irrespective of progressbar and process executing. I would appreciate code changes because i am new to python and PYQT5.

Thanks in advance.

from PyQt5.QtGui import QMovie
import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import (QApplication, QDialog,
                             QProgressBar, QPushButton, QVBoxLayout, QLabel)
from multiprocessing import Pool, freeze_support

TIME_LIMIT = 100
def func(a, b):
    print(a +b)

class External (QThread):
    countChanged = pyqtSignal(int)

    def run(self):
        count = 0
        print("check1")
        while count < TIME_LIMIT:
            count += 1
            time.sleep(1)
            print("thread1 update")
            self.countChanged.emit(count)

class Actions (QDialog):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Progress Bar')
        self.setGeometry(200,300,200,200)
        col = QVBoxLayout(self)
        self.text = ['ON', 'OFF']
        self.label = QLabel(self)
        self.movie = QMovie("movie.gif")
        self.label.setMovie(self.movie)
        self.progress = QProgressBar(self)
        self.progress.setGeometry(0, 0, 300, 25)
        self.progress.setMaximum(100)
        self.button = QPushButton('Start', self)
        col.addWidget(self.label)
        col.addWidget(self.progress)
        col.addWidget(self.button)
        self.label.move(0,90)
        self.button.move(0, 30)
        self.button.clicked.connect(self.onButtonClick)
        self.show()

    def startAnimation(self):
        self.movie.start()

    def onButtonClick(self):
        self.startAnimation()
        self.calc = External()
        a_args = [1, 2, 3, 4, 5, 6]
        second_arg = 1
        self.calc.countChanged.connect(self.onCountChanged)
        self.calc.start()
        QApplication.processEvents()
        for i in a_args:
            pool = Pool(processes=3)
            M = pool.starmap(func, zip(a_args, repeat(second_arg)))
            QApplication.processEvents()
            pool.close()
            pool.join()
            print("value of args",i)

    def onCountChanged(self, value):
        self.progress.setValue(value)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Actions()
    sys.exit(app.exec_())
user3666197
  • 1
  • 6
  • 50
  • 92
shivangi
  • 1
  • 2
  • Well, `pool.starmap()` is a synchronous call, even if the `func` work occurs in other processes. You'd need to use `pool.apply_async()` and manually loop and poll for results while `QApplication.processEvents()`ing – or maybe use a timer instead to wait for results, so PyQT remains in control of the event loop. – AKX Feb 15 '22 at 11:35
  • (Or, considering you already have that `External` thread, maybe use it.) – AKX Feb 15 '22 at 11:38
  • tried both the methods pool.apply_async() and External thread . There is no change in behavior of code functionality. Still getting stuck. – shivangi Feb 16 '22 at 08:07
  • What did you do with the results of `pool.apply_async()`? Waited for them in a timer loop or external thread? – AKX Feb 16 '22 at 08:11

0 Answers0