I write multiprocessing program with PyDeadObjectError.
This code do “witetext” throgh queue.
I know problem point,but I can’t modify.
Please advice me.
“start_gui” function have problem point.
coding: UTF-8
import wx
import multiprocessing
import time
class MyFrame(wx.Frame):
def init(self, parent, id, title, queue):
wx.Frame.init(self, parent, id, title=title, size=(400, 400))
self.queue = queue
self.Bind(wx.EVT_CLOSE, self.onClose)
panel = wx.Panel(self, -1)
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
self.tc1 = wx.TextCtrl(panel, -1) #input part
hbox1.Add(self.tc1, 1)
vbox.Add(hbox1, 0, wx.EXPAND)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
ok_button = wx.Button(panel, -1, “OK”, size=(70, 30))
cancel_button = wx.Button(panel, -1, “Cancel”, size=(70, 30))
ok_button.Bind(wx.EVT_BUTTON, self.click_okbutton)
cancel_button.Bind(wx.EVT_BUTTON, self.click_cancelbutton)
hbox2.Add(ok_button, 0)
hbox2.Add(cancel_button, 0)
vbox.Add(hbox2, 0, wx.ALIGN_RIGHT)
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
self.tc2 = wx.TextCtrl(panel, -1, style = wx.TE_READONLY) #output part
hbox3.Add(self.tc2, 1, wx.EXPAND)
vbox.Add(hbox3, 1, wx.EXPAND)
panel.SetSizer(vbox)
self.Show(True)
def click_okbutton(self, event):
self.queue.put(self.tc1.GetValue())
self.tc1.Clear()
def click_cancelbutton(self, event):
self.tc1.Clear()
def onClose(self, event):
self.queue.put(None)
self.tc2.WriteText(“closing”)
self.Destroy()
def start_gui(ev, queue):
app = wx.App(False)
frame = MyFrame(None, -1, “sample.py”, queue)
app.MainLoop()
ev.set()
while True:
item = queue.get()
if item is None:
print “done!”
break
else:
frame.tc2.WriteText(item) #problem point!
def worker(queue):
while True:
queue.put(“on process\n”)
time.sleep(2)
if name == ‘main’:
multiprocessing.freeze_support()
queue = multiprocessing.Queue()
ev = multiprocessing.Event()
p = multiprocessing.Process(target = start_gui, args=(ev, queue,))
p.start()
p2 = multiprocessing.Process(target = worker, args=(queue,))
p2.start()
ev.wait()
p2.terminate()