暂停和恢复qthread
我正在使用python,pyqt5,qthread。
我有一个GUI,它显示图片并使用“继续”按钮更改它们。
我想达到的东西: 在另一个选项卡中,我有一个按钮“开始”。
按下“启动”按钮时,假设应预处理8个图片(更改了一个尺寸的大小)。 每次点击“继续”时,应显示一张图片,应预处理另一个新图片,并应删除第一张图片(qpixmap对象)。
因此,第一次运行将是: 图1-8预处理 图片1显示
第二次运行: 图9预处理 显示图2
为了不冻结GUI,我需要在这里线程。我的想法是在第一次运行中进行8张图片后,然后在每张预处理的图片中停止此线程,但永远不要破坏该过程。
您将如何意识到此线程暂停?
这样做的主要问题是,如果某人在“继续”上单击太快(示例:图片9仍在预处理中,但它已经试图开始对图片10进行预处理,但是我只想将图片10添加到图片中,以进行预处理,并且不要停止一些东西...)
您能帮我吗?
I am using Python, PyQt5, QThread.
I have a GUI, which displays pictures and changes them with a "Continue" Button.
What I want to reach:
In another tab I have a Button "Start".
When the "start" button is pressed, let's say 8 pictures should be preprocessed (changed in size an saved in a dict).
With every click on "continue", a picture should be shown another new picture should be preprocessed and the first picture (QPixmap object) should be deleted.
So first run would be:
picture 1-8 preprocessed
picture 1 displayed
Second run:
picture 9 preprocessed
picture 2 displayed
To not freeze the GUI, I will need a Thread here. My idea is to stop this thread after 8 pictures at the first run and then after every preprocessed picture but to never destroy the process.
How would you realize this thread pause?
The main problem with this will be if someone clicks too fast on "continue" (example: picture 9 is still preprocessing but its tried to already start preprocessing of picture 10, but i just want to add picture 10 to the pictures to be preprocessed and not to stop something...)
Can you help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般而言,您通常不需要“暂停”线程,而是根据所需的预紧计数正确 queue 。然后,该线程将“暂停”本身,但这仅仅是因为它正在等待新对象处理。
假设处理是始终顺序的,并且在一个方向上(不回到上一个图像),可能的解决方案是始终排队
n + preload-count
eyt时间显示新图像。相反,如果您想在两个方向上提供浏览/处理,那么事情变得有些棘手:问题是线程队列在处理时可能需要更改,因为用户已更改了浏览方向(甚至跳到另一个索引)。
一个可能的解决方案是为UI实施进一步的队列,以取决于当前索引。仅当从线程中收到新图像,然后仅基于当前索引排列下一个进程请求时,就会导致UI线程要求新进程。这种方法的一个好处是,即使在上面使用的更简单的“线性”方向上,它显然可以起作用。
Generally speaking, you don't usually need to "pause" the thread, but to properly queue the processing based on the needed preload count. The thread will then "pause" itself, but only because it's waiting for new objects to process.
Assuming that the processing is always sequential and in one direction (no going back to the previous image), a possible solution is to always queue the
n + preload-count
item every time a new image is shown.If you, instead, want to provide browsing/processing in both directions, things get a bit tricky: the problem is that the thread queue might need to change while it's processing because the user has changed browsing direction (or even skipped to another index).
A possible solution is to implement a further queue for the UI that depends on the current index. This results in the UI thread requesting a new process only when a new image has been received from the thread, and then queue the next process request only based on the current index. A benefit of this approach is that it would obviously work even for the simpler "linear" direction used above.