python班级排队,Python:无阻塞地排队

I have a question about queues. I am creating a GUI with wxpython, and in the program I need to do something in a separate thread. After the thread finished, I have to modify the gui. The GUI should not block while the other thread is running.

For this I thougt about queues and wrote something like this:

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

But the problem is now, that the program still blocks, I think because the queue is waiting for an item. I could start it in a new thread, but then I have to do the thing with the queue again, to execute the function in my main thread.

Can anybody help me?

Thank you in advance.

解决方案

I suggest you to use wx.CallAfter(). You may find some useful examples here. Also you can use pubsub module to send messages to your GUI. Then your GUI won't block due to other threads.

Here is a nice blog that I read when I had a similar issue like yours.

You can also find some other questions on SO based that may assist you to understand this concept here, here, & here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值