【问题标题】:Python: queue without blockingPython:无阻塞队列
【发布时间】:2014-05-02 09:37:58
【问题描述】:

我有一个关于队列的问题。我正在用 wxpython 创建一个 GUI,在程序中我需要在一个单独的线程中做一些事情。线程完成后,我必须修改 gui。 GUI 不应在其他线程运行时阻塞。 为此,我想到了队列并写了如下内容:

def do_something(gui):
    # Here it normally does something
    gui.queue.put(gui.new)

class GuiFrame(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, title="Test")
        self.queue = Queue.Queue()
        self.calculate()

    def calculate(self):
        thread = threading.Thread(target=do_something, args=(self,))
        thread.start()
        function = self.queue.get()
        function()

    def new():
        # Here something modifies the gui
        pass

但现在的问题是,程序仍然阻塞,我认为是因为队列正在等待一个项目。我可以在一个新线程中启动它,但是我必须再次对队列执行此操作,才能在我的主线程中执行该函数。 有谁能够帮助我? 提前谢谢你。

【问题讨论】:

    标签: python multithreading wxpython


    【解决方案1】:

    我建议你使用wx.CallAfter()。你可能会发现一些有用的例子here。您也可以使用pubsub 模块向您的 GUI 发送消息。然后你的 GUI 不会因为其他线程而阻塞。

    Here 是一个不错的博客,当我遇到与您类似的问题时,我阅读了该博客。 您还可以找到一些基于 SO 的其他问题,这些问题可能有助于您理解这个概念 hereherehere

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2011-03-27
      • 2015-08-12
      相关资源
      最近更新 更多