python - 快速使用(Canonical 的 python+gtk),如何在使用多处理时传递 self.ui 项目

标签 python ubuntu canonical-quickly

我一直在玩 Quickly(Canonical 创建的用于快速开发 Python + GTK 应用程序的程序),我希望我开始开发的这个特定应用程序等到至少一个界面“启动”并且有一个由 DHCP 分配给它的默认路由。

到目前为止,这是我的代码:

import gettext
import subprocess
import time
from multiprocessing import Process, Queue, current_process
from gettext import gettext as _
gettext.textdomain('launcher')

import gtk
import logging
logger = logging.getLogger('launcher')

from launcher_lib import Window
from launcher.AboutLauncherDialog import AboutLauncherDialog
from launcher.PreferencesLauncherDialog import PreferencesLauncherDialog

# See launcher_lib.Window.py for more details about how this class works
class LauncherWindow(Window):
    __gtype_name__ = "LauncherWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(LauncherWindow, self).finish_initializing(builder)

        self.AboutDialog = AboutLauncherDialog
        self.PreferencesDialog = PreferencesLauncherDialog

        self.ui.status_label.set_text("Waiting for the interface to come up")
        while True:
            if subprocess.call(["/sbin/ifconfig | grep Bcast"], shell=True) == 0 and subprocess.call(["netstat -rn | grep UG | grep 0.0.0.0"], shell=True) == 0:
                break
        self.ui.status_label.set_text("Loaded")

# There is more code after this, but this should be all that I 
# need to get the GUI to show a status line saying the interface
# is up... right???

现在,这一切都很好,但是它阻止了应用程序的启动,所以看起来它只是挂起,直到进程返回 true。

我想做的是把它推到一个单独的进程中,但是,我对 Python 有点 ^h^w^h 很多新手,虽然我找到了多处理库,但我似乎没有能够将 self.ui.status_label 推送到我可以从生成的进程中重复使用的任何内容中。

例如,我是否定义这样的变量:
# after line:
logger = logging.getLogger('launcher')
# add this
status_label = None

然后像这样引用它:
# after line:
self.PreferencesDialog = PreferencesLauncherDialog
# add this
status_label = self.ui.status_label
# Then set it like this:
status_label.set_text("Waiting for the interface to come up")

或者,我应该创建一个只处理更新状态窗口的新类(如果是这样,我应该怎么做),或者......我应该在我想设置它的时候抛出 self.ui.status_label 吗?我试过这样做:
def update_status(me):
    me.ui.status_label.set_text("Update me!")
    return True

# And then in the finish_initializing() code
update_status(self)

但这只是说
NameError: global name 'self' is not defined

我正在尽我最大的努力,但我现在很困惑,而且,正如我所提到的,Python 不是我完全熟悉的语言,但我正在试一试:)

PS,我认为这应该有“快速”标签,但是,我还没有创建该标签的声誉。

最佳答案

我会使用线程模块而不是多处理模块:http://docs.python.org/library/threading.html

这样做的原因是新线程将在与主程序相同的上下文中执行,因此在子进程将具有不同的上下文时交换数据会容易得多。这样做的方法是创建一个提供 run() 方法的小类(比如 InterfaceListener)。然后在 InterfaceListener 类的构造函数中,确保传递标签并存储它。然后在 run 方法中,等待界面出现,当它出现时,更新标签。就像是:

class InterfaceListener(object):
    def __init__(self, label):
        self.__label = label

    def run(self):
        while True:
            if  subprocess.call(["/sbin/ifconfig | grep Bcast"], shell=True) == 0 and
                subprocess.call(["netstat -rn | grep UG | grep 0.0.0.0"], shell=True) == 0:
                break
        self.__label.set_text("Loaded")

然后在上面的finish_initializing方法中,就在第一个self.ui.status_label.set_text之后,添加如下内容:
t = threading.Thread(target=InterfaceListener(self.ui.status_label))
t.start()

我还没有测试过这个,所以你可能需要适应。另一种选择是使用 Timer 对象定期检查接口(interface),而不是使用 while True 循环。

关于python - 快速使用(Canonical 的 python+gtk),如何在使用多处理时传递 self.ui 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8159635/

相关文章:

Python: Nose 不接受单元测试

python - PyGtk CellRendererCombo 使用 Pixbuf

android - 启动 avd 时出现 Ubuntu 14.04 Android Studio Panic 错误

安卓/Linux : Error of failed request: BadAlloc (insufficient resources for operation)

python - Ubuntu快速(python/gtk) - 如何监控stdin?

python - 将变量传递给对话框

python - 向 Python enum.IntEnum 添加属性和起始值

python - 连接多个数据帧

python - 对现有作业流程运行引导操作

linux - oprofile 二进制构建错误 -(未找到自由库)