pygtk应用程序中的单独线程

发布于 2024-07-15 18:09:03 字数 998 浏览 5 评论 0原文

我在线程化 pyGTK 应用程序时遇到一些问题。 我给线程一些时间来完成其任务,如果出现问题我只是继续但警告用户。 但是,一旦我继续,该线程就会停止,直到调用 gtk.main_quit 为止。 这让我很困惑。

相关代码:

class MTP_Connection(threading.Thread):
    def __init__(self, HOME_DIR, username):
        self.filename = HOME_DIR + "mtp-dump_" + username
        threading.Thread.__init__(self)

    def run(self):
        #test run
        for i in range(1, 10):
            time.sleep(1)
            print i   

........................

start_time = time.time()
conn = MTP_Connection(self.HOME_DIR, self.username)
conn.start()
progress_bar = ProgressBar(self.tree.get_widget("progressbar"),
                           update_speed=100, pulse_mode=True)
while conn.isAlive():
    while gtk.events_pending():
        gtk.main_iteration()
    if time.time() - start_time > 5:
        self.write_info("problems closing connection.")
        break
#after this the program continues normally, but my conn thread stops

I'm having some problems threading my pyGTK application. I give the thread some time to complete its task, if there is a problem I just continue anyway but warn the user. However once I continue, this thread stops until gtk.main_quit is called. This is confusing me.

The relevant code:

class MTP_Connection(threading.Thread):
    def __init__(self, HOME_DIR, username):
        self.filename = HOME_DIR + "mtp-dump_" + username
        threading.Thread.__init__(self)

    def run(self):
        #test run
        for i in range(1, 10):
            time.sleep(1)
            print i   

..........................

start_time = time.time()
conn = MTP_Connection(self.HOME_DIR, self.username)
conn.start()
progress_bar = ProgressBar(self.tree.get_widget("progressbar"),
                           update_speed=100, pulse_mode=True)
while conn.isAlive():
    while gtk.events_pending():
        gtk.main_iteration()
    if time.time() - start_time > 5:
        self.write_info("problems closing connection.")
        break
#after this the program continues normally, but my conn thread stops

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2024-07-22 18:09:03

首先,不要子类化threading.Thread,而使用Thread(target=callable).start()

其次,您明显阻塞的原因可能是 gtk.main_iteration 接受参数 block,默认为 True,因此您的调用当没有要迭代的事件时,gtk.main_iteration 实际上会阻塞。 这可以通过以下方法解决:

gtk.main_iteration(block=False)

但是,没有真正的解释为什么您要使用这个破解循环而不是实际的 gtk 主循环。 如果您已经在主循环中运行它,那么我建议您做错了事情。 如果您给我们提供更多细节和/或完整示例,我可以扩展您的选择。

第三,这只是后来才出现的:始终总是总是确保您已调用gtk.gdk.threads_init 在任何带有线程的 pygtk 应用程序中。 GTK+ 在运行线程时有不同的代码路径,它需要知道如何使用这些路径。

我写了一篇关于 pygtk 和线程的小文章,其中提供了你是一个小抽象,所以你永远不必担心这些事情。 该帖子还包括一个进度条示例。

Firstly, don't subclass threading.Thread, use Thread(target=callable).start().

Secondly, and probably the cause of your apparent block is that gtk.main_iteration takes a parameter block, which defaults to True, so your call to gtk.main_iteration will actually block when there are no events to iterate on. Which can be solved with:

gtk.main_iteration(block=False)

However, there is no real explanation why you would use this hacked up loop rather than the actual gtk main loop. If you are already running this inside a main loop, then I would suggest that you are doing the wrong thing. I can expand on your options if you give us a bit more detail and/or the complete example.

Thirdly, and this only came up later: Always always always always make sure you have called gtk.gdk.threads_init in any pygtk application with threads. GTK+ has different code paths when running threaded, and it needs to know to use these.

I wrote a small article about pygtk and threads that offers you a small abstraction so you never have to worry about these things. That post also includes a progress bar example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文