S
S
Semyon Beloglazov2017-07-25 13:51:54
Python
Semyon Beloglazov, 2017-07-25 13:51:54

Hangs gui (pyqt5) when executing while / time.sleep. What to do?

When you click on the button, you need to execute the required number of queries in the while loop, here is the code (the block of code itself, which is executed when the button is clicked):

# Начало залива
    def startJob(self):
        # Логи
        self.addLog('Сохраняем настройки...')
        
        # Настройки и переменные
        self.isJobStopped = False # Начинаем работу
        incr = 1 # Счетчик
        
        while incr <= self.spinBox.value(): # Начинаем выполнять запросы
            if self.isJobStopped == False:
                send_request = requests.post('https://youtube.com/example')
                self.addLog('Выполнено запросов: ' + str(incr) + '/' + str(self.spinBox.value()))
                incr += 1
            else:
                break
        self.addLog('Работа окончена.')

If similar code is simply executed in cmd, then it will normally display logs with each request. If such code is executed in the context of PyQt5, then the program hangs until the entire while is executed and at the end immediately spits out all the logs at once.
Why is this happening? I read on the Internet that such constructions interfere with the flow of the GUI, which causes a hang. How to get around this, what needs to be done? I would be very grateful for an answer.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Semyon Beloglazov, 2017-07-26
@Batlab

The problem seems to be solved. With grief on the floor, I created a separate thread of the QThread object, in which, at startup, all actions were performed and signals were sent to the slot in the UI thread, where they were already processed. The topic can be considered closed.
Ps this topic helped here https://nikolak.com/pyqt-threading-tutorial/

D
Dmitry, 2017-07-25
@LazyTalent

QTime or looping in another thread

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question