PYQT延迟功能的计算而无需阻止主线程
我继承了一个构建类似的GUI代码: 任何按钮信号都会触发一个插槽,这些插槽然后调用外部进程以接收信息并等到该过程完成,然后插槽继续进行。问题是,此外部过程需要0.5到60秒,而GUI冻结了。我正在努力寻找一种很好的方法,将此过程分开为其他线程或QProcess(这样我就不会阻止主事件循环),然后返回并继续从同一点继续使用相关的插槽(或函数)从该外部慢速过程中收到的信息。发电机似乎应该放在这里,但是我正在努力弄清楚如何重组代码,这样就可以了。 有什么建议或想法吗?是否有QT的方法来“产生”功能,直到该过程完成然后继续该功能?
当前结构的PSUDO代码:
button1.clicked.connect(slot1)
button2.clicked.connect(slot2)
def slot1():
status = subprocess.run("external proc") # this is blocking
...
...
return
def slot2():
status = subprocess.run("external proc") # this is blocking
...
...
return
I've inhereted a GUI code which is structured something like this:
any button signal triggers a slot, those slots then call an external process to receive information and wait until that process finishes, then the slot proceeds. the issue is, this external process takes between 0.5 to 60 seconds, and in that time the GUI freezes. i'm struggling to find a good way to seperate this process call to a different thread or QProcess (that way i will not block the main event loop) and then return and continue the relevent slot (or function) from that same point with the information received from that external slow process. generators seem like something that should go here, but im struggling to figure how to restructure the code so this will work.
any suggestions or ideas? is there a Qt way to "yield" a function until that process completes and then continue that function?
Psudo code of the current structure:
button1.clicked.connect(slot1)
button2.clicked.connect(slot2)
def slot1():
status = subprocess.run("external proc") # this is blocking
...
...
return
def slot2():
status = subprocess.run("external proc") # this is blocking
...
...
return
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我在评论中提到的示例的代码:
Here is the code with the example I was mentioning in the comments:
您可以使用Qthread。使用QThread,您可以将参数传递给MainWindow中的功能,并具有信号机制。
Here is a source that explains how to use Qthread:
https://realpython.com/python-pyqt- 我认为, qthread/
如果您阅读了伴侣,它将对您有所帮助。页面上有一个示例GUI,我将其写给您(您可以运行):
You can use QThread. With Qthread you can pass arguments to a function in mainWindow with signal mechanism.
Here is a source that explains how to use Qthread:
https://realpython.com/python-pyqt-qthread/
if you read the soruce it will be helpfull to you, i think. And there is a sample gui in the page, i write it down to you(you can run it):
通常,我要做的是按下按钮运行一个函数,该功能启动线程为我完成工作。
在我的示例中,我有3个按钮。一个等待一秒钟,另一个等待5,另一个等待10。
我将按钮插槽单击时连接threeed_wait(),并且我使用lambda,因为我想通过该方法将整数参数传递到多长时间等待(在此示例中等待只是假处理时间)。
然后,我拥有实际_WAIT()方法,即实际等待的代码,该代码由线程执行。由于有一个线程运行该代码,因此启动线程后,主GUI事件循环立即退出螺纹_WAIT()方法,并且可以继续其事件循环,
从而阻止我的GUI冻结。
编辑:
对不起,对于您的问题的第二部分,如果您想等待线程完成之前完成其他操作,则可以使用这样的标志:
并检查self.done flag无论您在哪里需要它。
这取决于您通过等待完成的意思。
我认为,如果您使用Qthread,则在完成线程后也可以发出信号,并将该信号连接到此后的任何插槽,但是我没有使用Qthread。
Usually what I do is have the button press run a function that launches a thread to do the work for me.
In my example I have 3 buttons. One that waits for one second, another that waits for 5, and another that waits for 10.
I connect the button slots when they are clicked to threaded_wait() and I use lambda because I want to pass that method an integer argument on how long to wait for (Waiting in this example is just fake processing time).
Then I have the method actual_wait() which is the code that is actually waiting, which is being executed by the thread. Since there is a thread running that code, the main GUI event loop exits the threaded_wait() method right after starting the thread and it is allowed to continue it's event loop
This prevents my GUI from freezing up.
EDIT:
Sorry as for the second part of your question, if you want to wait for the thread to finish before doing something else, you can use a flag like this:
And check that self.DONE flag wherever you need it.
It kind of depends what you mean by wait for it to complete.
I think if you use QThread you can also emit a signal when the thread is done and connect that signal to whatever slot after that, but I haven't used QThread.