Vanitas Vanitas - 1 month ago 12
Python Question

Python Multiprocessing with Frame

Currently working on a project that requires multi threading to interact with a physical device while having an active frame open to send instructions.

The problem is that when the Frame is spawned and the "Hello" button is pressed, the

test
method is ran and the
Process
running
LEDBehavior.BlinkYellowLightEverySecond ()
is properly being ran, but the button remains in it's pressed graphical state and the window becomes unresponsive.

Here is the code for this frame.

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 ()

Answer

This is because of:

p1.join()

You're telling your main process to wait on p1. BlinkYellowLightEverySecond seems like the type of function that may run forever until stopped, and I'm guessing you don't want to halt your program for this. Just remove the join and add a button for stopping/changing the blink.

EDIT

Second error I missed:

p1 = Process (LEDBehavior.BlinkYellowLightEverySecond ())

the tuple () after Blink... actually makes the call, so your invoking the function in your main process! Remove the (), and invoke the constructor properly:

p1 = Process (target = LEDBehavior.BlinkYellowLightEverySecond)

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