简体   繁体   English

Python线程处理-更新GUI时崩溃

[英]Python Threading--Crash when updating the GUI

So here is my problem-- 所以这是我的问题

I am creating a python GUI application using wx python. 我正在使用wx python创建一个python GUI应用程序。 Once the process button is clicked, there is some file creation/encoding that goes on behind the scenes. 单击处理按钮后,将在后台进行一些文件创建/编码。 Initially, this froze EVERYTHING on the GUI while it was working, so I decided to do the file creation/encoding on a separate thread--Here is the rub, as soon as the method that created the thread finishes--(Relatively quickly) Here is the code that actually creates the threads: 最初,它在工作时冻结了GUI上的所有内容,因此我决定在单独的线程上进行文件创建/编码-这就是麻烦,一旦创建线程的方法完成,就可以了-(相对较快)这是实际创建线程的代码:

for audiobook in AudioBookObjects:
        thread.start_new(self.createSingleBook, tuple([audiobook]))

So here is the weird part--on one machine that I used this code let one thread go, finish and come back without freezing the gui. 所以这是很奇怪的部分-我使用此代码的一台机器上放开了一个线程,完成并返回,而没有冻结gui。 On another machine, it shoots off two threads really quickly and they crash when they terminate! 在另一台计算机上,它真的很快就触发了两个线程,并且在终止时崩溃! Does something change the behavior of threads between Operating systems? 某些事情会改变操作系统之间线程的行为吗? I am using the same version of python. 我正在使用相同版本的python。 I am ABSOLUTELY sure of that. 我绝对相信。

Basically, I need to know two things: What happen to python threads after the method that created them ends, and how can I build a GUI that does not freeze if the trigger method HAS to stick around for the threads to end gracefully!? 基本上,我需要知道两件事:创建线程的方法结束后python线程会发生什么,以及如果触发方法必须坚持执行以使线程正常结束,那么如何构建不会冻结的GUI?

Thanks in advance! 提前致谢! Let me know if you need anymore information 让我知道您是否需要更多信息

If i remeber correctly, windows does "real" threads, where in Linux threads are processes. 如果我正确地记得,Windows会执行“真实”线程,而在Linux线程中就是进程。 The difference is, that normal threads share the same memory and processes do not. 不同之处在于,普通线程共享相同的内存,而进程不共享。

I would guess your program crashes under win and runs under linux. 我猜你的程序在win下崩溃并在linux下运行。 If i'm not mistaken it's likely that your createSingleBook-threads accesses the same object, which doesn't seem to have a lock-protection. 如果我没记错的话,很可能您的createSingleBook-threads访问了同一个对象,该对象似乎没有锁定保护。

With a real fork, everything is duplicated if not explicitly stated otherwise. 如果没有明确说明,使用实叉,所有内容都会重复。 So you would have some more protection against lock and race conditions. 因此,您将获得更多针对锁定和竞争条件的保护。

Just my preliminary guess, anyone with greater insight feel free to correct me. 只是我的初步猜测,任何有更深刻见识的人都可以随时纠正我。 Otherwise i would recommend to implement a lock on the object you are trying to manipulate. 否则,我建议对您要操作的对象实施锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM