wxPython wx.CallAfter - 如何让它立即执行? [英] wxPython wx.CallAfter - how do I get it to execute immediately?

查看:2480
本文介绍了wxPython wx.CallAfter - 如何让它立即执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用wx.CallAfter执行一个函数时,在其中设置一个变量。我想要能够在下一行获取该变量的值,但是CallAfter似乎稍后执行一个LOT函数。我认为它会在稍后处理的某个队列中推送它,或者有什么...有没有办法让该队列立即被处理?

When I execute a function with wx.CallAfter, inside it a variable is set. I want to be able to get the value of that variable on the next line, however CallAfter seems to execute the function a LOT later on. I think it pushes it in some queue that gets processed later or something... Is there a way to get that queue to be processed immediately?

这是代码wx.CallAfter:

This is the code of wx.CallAfter:

def CallAfter(callableObj, *args, **kw):
    """
    Call the specified function after the current and pending event
    handlers have been completed.  This is also good for making GUI
    method calls from non-GUI threads.  Any extra positional or
    keyword args are passed on to the callable when it is called.

    :see: `wx.CallLater`
    """
    assert callable(callableObj), "callableObj is not callable"
    app = wx.GetApp()
    assert app is not None, 'No wx.App created yet'

    if not hasattr(app, "_CallAfterId"):
        app._CallAfterId = wx.NewEventType()
        app.Connect(-1, -1, app._CallAfterId,
                    lambda event: event.callable(*event.args, **event.kw) )
    evt = wx.PyEvent()
    evt.SetEventType(app._CallAfterId)
    evt.callable = callableObj
    evt.args = args
    evt.kw = kw
    wx.PostEvent(app, evt)

我的假设是wx.PostEvent将'evt'在app的某些内部容器中,在某个时候容器被迭代并且所有元素都被执行可调用?所以基本上我需要立即发生这种情况,但我找不到任何看起来像'wx.ForceEventProcessing()'或'wx.FlushEventsQueue'的任何东西

My assumption is that wx.PostEvent puts 'evt' in some internal container in 'app' and at some point that container is iterated and all elements have their 'callable' executed or something? So basically I need to cause this to happen immediately, but I can't find anything that looks like 'wx.ForceEventProcessing()' or 'wx.FlushEventsQueue'

我尝试在定义方法的面板中调用self.Update(),但是刚刚阻止了应用程序并停止响应。

I tried calling self.Update() in the panel where the methods are defined, but that just blocked the application and it stopped responding.

推荐答案

p>如你所猜, wx.CallAfter 将呼叫信息添加到队列中(挂起的事件队列,在下一次事件队列是GUI线程中处理的事件队列清空),然后立即返回。换句话说,它是一个火和忘记函数调用。

As you've guessed wx.CallAfter adds the call info to a queue (the pending event queue, which is processed in the GUI thread the next time the event queue is emptied) and then returns immediately. In other words, it is intended to be a "fire and forget" function call.

如果你需要得到所谓的函数的结果,有几种方法可以服用一个是延迟的结果模式,其中基本上你得到一个结果对象,它将在调用后接收函数调用的结果,并且调用代码可以在发生时收到通知。在 wx.lib.delayedresult 中有一个实现,并且可能在不同的地方找到这种模式的其他实现。这个方法的好处是,调用线程不必停止并等待结果,如果需要,可以继续处理其他事情。

If you need to get the result of that called function there are a few approaches that can be taken. One is the delayed result pattern, where basically you get a result object that will receive the result of the function call after it has been called, and the calling code can get a notification when that happens. There is an implementation of this in wx.lib.delayedresult and probably other implementations of this pattern can be found in various places. The nice thing about this approach is that the calling thread doesn't have to stop and wait for the result and can continue processing other things if desired.

另一种方法是在我看来,相当不错的是 wxAnyThread ,可以在 https://pypi.python.org/pypi/wxAnyThread/ 。该模块提供了一个装饰器,允许从正常函数调用的任何线程调用方法,但是当从GUI线程以外的其他线程调用时,它将使用 wx.CallAfter - 像处理在UI线程中调用的函数,然后等待结果,并在收到时返回给调用者。因此,尽管调用线程在UI事件被处理并被调用的时候被阻塞,但使用起来简单得多,并且降低了主叫侧的复杂性。

Another approach that is quite nice in my opinion is wxAnyThread, which can be found at https://pypi.python.org/pypi/wxAnyThread/. This module provides a decorator that allows methods to be called from any thread like a normal function call, but when called from something other than the GUI thread it will use wx.CallAfter-like processing to have the function called in the UI thread, and then wait for the result and will return it to the caller when it is received. So although the calling thread is blocked while the UI events are processed and the function is called, it is much simpler to use and reduces complexity on the calling side.

这篇关于wxPython wx.CallAfter - 如何让它立即执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆