python - pyGTK running 2 "main loops" at once -
i writing pygtk program (among other things) allow user select , run second program list. start program user selects separate process it's own main loop, i'm not sure safe thing do. ran quick test (see below), , seemed safe, don't lot of gui programming know there might things i'm not thinking of. couldn't find tutorials addressing this, thought i'd ask before deep in.
the 2 programs not need talk once second 1 has started.
here's test tried.
import gtk import numpy np class topgui(gtk.window): def __init__(self): super(topgui,self).__init__() parentlabel = gtk.label('i father') newwindowbtn = gtk.button('meet brother') vbox1 = gtk.vbox(false,4) vbox1.pack_start(parentlabel, true, true, 4) vbox1.pack_start(newwindowbtn, true, true, 4) newwindowbtn.connect("clicked", self.on_clicked) # 0p3n br07h3r loop on butt0n pre55 self.add(vbox1) self.show_all() self.startitup def on_clicked(self, lebutton): browin = secondgui() browin.startitup def startitup(self): gtk.main() class secondgui(gtk.window): bigthing = none def __init__(self): super(secondgui,self).__init__() brotherlabel = gtk.label('i 3\/1l u|\|kl3. d3v0ur m3m0ry!') spinner = gtk.spinner() vbox1 = gtk.vbox(true,4) vbox1.pack_start(brotherlabel, true, true, 4) vbox1.pack_start(spinner, true, true, 4) spinner.start() self.add(vbox1) self.show_all() self.bigthing = np.zeros([10000,10000]) def startitup(self): gtk.main() fatherloop = topgui()
for posterity, here improved version of test code implementing abarnert's solution.
import gtk import numpy np import multiprocessing class topgui(gtk.window): def __init__(self): super(topgui,self).__init__() parentlabel = gtk.label('i father') newwindowbtn = gtk.button('meet brother') vbox1 = gtk.vbox(false,4) vbox1.pack_start(parentlabel, true, true, 4) vbox1.pack_start(newwindowbtn, true, true, 4) newwindowbtn.connect("clicked", self.on_clicked) # 0p3n br07h3r loop on butt0n pre55 self.connect("destroy", gtk.main_quit) self.add(vbox1) self.show_all() self.startitup() def on_clicked(self, lebutton): self.childprocess = multiprocessing.process(target=child_main) self.childprocess.start() def startitup(self): gtk.main() class secondgui(gtk.window): bigthing = none def __init__(self): super(secondgui,self).__init__() brotherlabel = gtk.label('i 3\/1l u|\|kl3. d3v0ur m3m0ry!') spinner = gtk.spinner() vbox1 = gtk.vbox(true,4) vbox1.pack_start(brotherlabel, true, true, 4) vbox1.pack_start(spinner, true, true, 4) spinner.start() self.add(vbox1) self.show_all() self.bigthing = np.zeros([10000,10000]) self.connect("destroy", gtk.main_quit) self.startitup() def startitup(self): gtk.main() def parent_main(): fatherloop = topgui() def child_main(): browin = secondgui() if __name__ == '__main__': parent_main()
i start program user selects separate process it's own main loop, i'm not sure safe thing do.
yes, safe, , in fact it's want. separate processes don't share unless go out of way (by fd inheritance, shared memory, pipes, etc.).
but code isn't doing that, or trying that. there's nothing in there start child process; create new object in same interpreter. so, start second gui, end running own gtk.main()
, making first gui hang until second 1 quits.
but you're referencing self.startitup
(and browin.startitup
) everywhere instead of calling self.startitup()
. second gui never starts up, , "safe" in trivial sense doing nothing @ safe.
you want use multiprocessing
. first, can't fatherloop = topgui()
@ top level, because every process may end running it. (whether implementation/platform-dependent.) then, need explicitly create process
, start child. so, this:
class topgui(gtk.window): # ... def on_clicked(self, lebutton): self.child = multiprocessing.process(target=child_main) self.child.start() # ... def parent_main(): fatherloop = topgui() def child_main(): browin = secondgui() if __name__ == '__main__': parent_main()
i'm assuming you're going fix code constructing topgui()
or secondgui()
calls self.startitup()
.
Comments
Post a Comment