Need to communicate update events to multiple running instances of the same python script

I need to communicate update events to all running instances of my python script, and i would like to keep the code as simple as possible. I have zero experience with communicating between running processes. Up until now, i have been reading/writing configuration files, which each instance will read and/or update.

Here is some pseudo code i have written (sort of a simple template) to wrap my head around how to solve this problem. Please try to help me fill in the blanks. And remember, i have no experience with sockets, threads, etc...

import process # imaginary module

class AppA():
    def __init__(self):
        # Every instance that opens will need to attach
        # itself to the "Instance Manager". If no manager
        # exists, then we need to spawn it. Only one manager
        # will ever exist no matter how many instances are
        # running.
        try:
            hm = process.get_handle(AppA_InstanceManager)
        except NoSuchProgError:
            hm.spawn_instance(AppA_InstanceManager)
        finally:
            hm.register(self)
        self.instance_manager = hm

    def state_update(self):
        # This method won't exist in the real code, however,
        # it emulates internal state changes for the sake of
        # explaination.
        #
        # When any internal state changes happen, we will then
        # propagate the changes outward by calling the
        # appropriate method of "self.instance_manager".
        self.instance_manager.propagate_state()

    def cb_state_update(self):
        # Called from the "Instance Manager" only!
        #
        # This may be as simple as reading a known
        # config file. Or could simply pass data
        # to this method.


class AppA_InstanceManager():
    def __init__(self):
        self.instances = []

    def register_instance(self, instance):
        self.instances.append(instance)

    def unregister_instance(self, instance):
        # nieve example for now.
        self.instances.remove(instance)

    def propagate_state(self):
        for instance in self.instances:
            instance.cb_state_update(data)

if __name__ == '__main__':
    app = AppA()

Any Suggestions?

Answers


There are a few options for this kind of design.

You could use a message queue, its made for this kind of stuff, e.g. AMQP or some ZeroMQ or something like it.

Or you could use something like Redis or some other (in-memory) database for synchronization.

If you don't want to use something like that, you could use the multiprocessing modules synchronization stuff.

Or use a platform specific IPC system, e.g. shared memory via mmap, sysv sockets, etc.

If you want to do things the way you explained, you could have a look at Twisteds perspective broker.


Need Your Help

node.js empty reply from the server

javascript node.js curl

I have developed an API using node.js. In my local system API is running at port 2100, and i used the following curl command

QVision Widget Error upon compile

qt compiler-errors widget qwidget

Only one error to go until I get to use this for my research!