Python 框架多处理

Python Multiprocessing with Frame

目前从事的项目需要多线程与物理设备交互,同时打开活动框架以发送指令。

问题是当生成框架并按下 "Hello" 按钮时,test 方法是 运行 而 Process 运行 LEDBehavior.BlinkYellowLightEverySecond () 正确地是 运行,但按钮仍处于按下图形状态并且 window 变得无响应。

这是这个框架的代码。

import LEDBehavior

class MainFrame (Frame):

    def __init__(self, master=None):

        Frame.__init__(self, master)
        self.pack()
        self.createWidgets ()

    def createWidgets(self):

        self.hi_there = Button(self)
        self.hi_there["text"] = "Hello",
        self.hi_there["command"] = self.test

        self.hi_there.pack({"side": "left"})

    def test (self):

        p1 = Process (LEDBehavior.BlinkYellowLightEverySecond ())
        p1.start ()
        p1.join ()

# create the application
myapp = MainFrame ()

#
# here are method calls to the window manager class
#
myapp.master.title ("Sample Text inc, Security Software Professionals")
myapp.master.maxsize (1920, 1080)
myapp.master.minsize (800, 640)

# start the program
myapp.mainloop ()

这是因为:

p1.join()

你告诉你的主进程等待 p1。 BlinkYellowLightEverySecond 似乎是一种可能 运行 永远停止的函数类型,我猜你不想为此停止你的程序。只需删除 join 并为 stopping/changing 闪烁添加一个按钮。

编辑

我错过的第二个错误:

p1 = Process (LEDBehavior.BlinkYellowLightEverySecond ())

Blink 之后的元组 ()...实际上进行了调用,因此您在主进程中调用该函数!删除(),并正确调用构造函数:

p1 = Process (target = LEDBehavior.BlinkYellowLightEverySecond)

参见 https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Process