[pygtk] Tow instance of the same pyGTK application in a the same
process
Mystilleef
mystilleef at gmail.com
Fri Jan 26 02:06:57 WST 2007
On 1/25/07, Thomas Güttler <hv at tbz-pariv.de> wrote:
> Am Donnerstag, 25. Januar 2007 02:00 schrieb Mystilleef:
> > Multiple instances of your application can share the same mainloop. All
> > you need to do is design an "InstanceManager" class that creates new
> > instances of your application. "InstanceManager" runs on the mainloop.
> > "InstanceManager" should have functions to create and keep track of all
> > instances of your application, perhaps stored in a list. When there are
> > no more instances of your application in the list, just quit the
> > mainloop. There is no threading needed. Of course each instance of
> > your application will have a reference to "InstanceManager" and the
> > methods (e.g. new_window, get_instances, focus_window, etc) it exposes.
> > Thus, "InstanceManager" becomes a mediator, and you can use it to
> > communicate with other instances, through the methods it exposes. I've
> > used this pattern successfully in one of projects.
>
> Hi!
>
> Sounds interesting. But I don't understand it.
> How do the instances (processes) communicate? How does the
> event get passed to the child process?
>
> Thomas
Hello,
There are no child processes. Everything runs in the same process and
mainloop. Let me give an example.
========================================================================
class InstanceManager(object):
"""
This class creates and manages all instances of MyApplication and
allows them to communicate with each other.
"""
def __init__(self):
# A list containing instances of MyApplication
self.__instances = []
def new_app(self):
# This method creates a new instance.
MyApplication(self)
return
def register_app(self, myApp):
# When a new instance is successfully created, register it with
# InstanceManager.
self.__instances.append(myApp)
return
def unregister_app(self, myApp):
# When an instance is no longer useful unregister it from
# InstanceManager.
self.__instances.remove(myApp)
if not self.__instances:
# Quit mainloop and process when no more instances are
# available.
self.__quit()
return
def get_instances(self):
# Return all running instances of myApp.
return self.__instances
def get_instance_ids(self):
# Return a unique identification for all instances of
# application.
return [myApp.id for myApp in self.__instances]
def focus_app(self, myAppId):
# Focus a particular instance of myApp.
for myApp in self.__instances:
if myApp.id == myAppId:
myApp.focus()
break
return
def __quit(self):
# Quit process.
from gtk import main_quit
main_quit()
return
class MyApplication(object):
"""
This creates an instance of my application.
"""
def __init__(self, InstanceManager):
self.id = id(self)
self.__InstanceManager = InstanceManager
self.__create_widgets()
self.__create_window()
self.__arrange_widgets()
# Assume everything goes fine, register with InstanceManager
self.__InstanceManager.register_app(self)
# Show myApp
self.__show_app()
def create_new_app(self):
# Use "InstanceManager" to create new instances.
self.__InstanceManager.new_app()
return
def focus_app(self, id):
# Use "InstanceManager" to focus other instances.
self.__InstanceManager.focus_app(id)
return
def focus(self):
# Add code to focus this app.
return
def __quit(self):
# Add clean up code and unregister from "InstanceManager"
self.__InstanceManager.unregister_app(self)
return
if __name__ == "__main__":
InstanceManager().new_app()
from gtk import main
main()
========================================================================
As you can see from the code snippet, you create new instances of
your application via the "new_app" method in "InstanceManager". All
instances of your application have a reference to "InstanceManager"
upon initialization. This allows them to communicate with each other
via methods in "InstanceManager".
For example, if you want to focus an instance, call the "focus_app"
method in "InstanceManager". Or if you want to create a new instance,
call the "new_app" method. Again, you should be able to call any
method in "InstanceManager" since each instance has a reference to it.
Also note that "InstanceManager" and all instances of you application
share the same mainloop and process. __Only__ InstanceManager is
reponsible for quiting the mainloop. Instances of your application
only need to register and unregister with InstanceManager. They don't
deal with the mainloop or care about it. There are no external
processes, threads or events involved. Just communication via good old
methods called message passing.
Once again, no external processes, no threading, no events. Everything
runs in the same mainloop and process. Communication is done via
methods in the mediator class, "InstanceManager".
More information about the pygtk
mailing list