コード例 #1
0
ファイル: mswitch.py プロジェクト: jldupont/pymultiproc
 def _hpump(self):
     """ Pulls message(s) (if any) from the message queue
     
         Must be called periodically - it serves as "event loop"
         for the whole process (normally)
     
         A Child process:
         - "_sub"  : locally subscribe the Main process to msgType
                     This will send all local messages of msgType
                     up to the Main process where it can
                     be further distributed (multicasted)
                     
         - "_ready" : just pass along to Message Bus
         
         The Main process:
         - "_sub" : 
             - subscribe locally from Message Bus
             - send down to all Children in "split-horizon"
             
         - "_started" : just pass along on Message Bus
             
     """
     #print "mswitch._hpump: (%s) msg: %s" % (self._pname, "begin")
     
     Bus.publish(self, "%beat?")
     msg = self._getMsg()
     while (msg is not None) and (not self._term):
         self._processMsg(msg)
         msg=self._getMsgNoWait()
     Bus.publish(self, "%beat?")
コード例 #2
0
ファイル: mswitch.py プロジェクト: jldupont/pymultiproc
 def _hpumpChild(self, mtype, spname, msg):
     #print "** mswitch._hpumpChild: mtype(%s) spname(%s)" % (mtype, spname)
     
     if mtype=="_ready":
         #print "mswitch._hpumpChild: sending `_ready` on local Bus"
         Bus.publish(self, "_ready")
         return
     
     if mtype=="_sub":
         
         try:    msgtype=msg.pop(0)
         except:
             raise RuntimeError("missing `msgType` from `_sub` message")
         
         self._addSub(self.MAIN_PNAME, msgtype)
         #print "*** mswitch_hpumpChild: child(%s) subscribing to msgtype(%s)" % (self._pname, msgtype)
         return
     
     ## All other "system" messages are ignored
     if mtype.startswith("_"):
         return
     
     ## Finally, publish whatever message we receive
     ##  from the Main process: we should have subscribed
     ##  to these anyhow (unless of course there is a bug ;-)
     #print "mswitch._hpumpChild: mtype(%s) msg(%s)" % (mtype, msg)
     Bus.publish(self, mtype, *tuple(msg))
コード例 #3
0
ファイル: heart.py プロジェクト: jldupont/pymultiproc
    def _qbeat(self):
        if self._beat:
            if self._child:
                Bus.publish(self, "%beat", self.pname)
            else:
                Bus.publish(self, "beat", self.pname)

        self._beat=False ## atomic assignment
コード例 #4
0
ファイル: tmswitch.py プロジェクト: jldupont/pymultiproc
 def run(self):
     while not self._exitFlag:
         try:
             Bus.publish(None, "mswitch_pump")
         except Exception,e:
             print "*** (MAIN) Comm Exception!"
             Bus.publish(None, "log", "*** (MAIN) Comm exception: %s" % e)
             break
コード例 #5
0
ファイル: watchdog.py プロジェクト: jldupont/pymultiproc
 def _hstarted(self, pname):
     """ Generates `ready` message when all the
         Child processes report as being started
     """
     if self.is_child:
         return
     
     self._started.extend([pname])
     if len(self._started) == len(self._procs):
         Bus.publish(self, "_ready")
         self._ready=True
コード例 #6
0
ファイル: watchdog.py プロジェクト: jldupont/pymultiproc
    def _hbeat(self, *p):
        """ Heart-beat from Main process
        """
        self._beat_count=0
        
        if self._termSent:
            return

        if self._term:
            self._termSent=True
            Bus.publish(self, "%log", "warning", "SIGTERM received, proc(%s)" % self.pname)
            Bus.publish(self, "_sigterm", self.pname)
コード例 #7
0
ファイル: pman.py プロジェクト: jldupont/pymultiproc
 def _hstart(self):
     """ Handler for "start" message
         marking the debut of all registered processes
     """
     for proc_name in self._procs:
         procDetails=self._procs[proc_name]
         try:     proc=procDetails["proc"]
         except:  raise RuntimeError("procDetails require a `proc` entry")
             
         try:
             Bus.publish(self, "%log", "< starting process(%s)" % procDetails["name"])
             proc.start()
             Bus.publish(self, "%log", "> started process(%s)" % procDetails["name"])
         except Exception,e:
             raise RuntimeError("Exception whilst starting process (%s)" % e)
コード例 #8
0
ファイル: mswitch.py プロジェクト: jldupont/pymultiproc
 def _sendToSubscribers(self, mtype, spname, msgTail):
     msg=[mtype, spname]
     msg.extend(msgTail)
     subs=self._subs.get(mtype, [])
     #print "mswitch._sendToSubscribers: mtype(%s) subs: %s " % (mtype, subs)
     for pname in subs:
         if pname==spname:# or pname==self.MAIN_PNAME:
             continue
         
         if pname==self.MAIN_PNAME:
             Bus.publish(self, mtype, *tuple(msgTail))
         else:
             #print "mswitch._sendToSubscribers: mtype(%s) pname(%s) msg(%s)" % (mtype, pname, msg)
             q=self._getQueue(pname)
             q.put(msg)
コード例 #9
0
ファイル: mswitch.py プロジェクト: jldupont/pymultiproc
    def _hpumpMain(self, mtype, spname, msg):
        
        #print "@@ mswitch._hpumpMain: mtype(%s) spname(%s)" % (mtype, spname)
        
        if mtype=="_started":
            Bus.publish(self, "started", spname)
            return
            
        if mtype=="_sub":
            try:    msgtype=msg.pop(0)
            except:
                raise RuntimeError("missing `msgType` from `_sub` message")           
            self._addSub(spname, msgtype)

            #print "*** mswitch._hpumpMain: subscribing to msgtype: ", msgtype
            
            ## repeat source message
            self._sendSplitHorizon(mtype, spname, [msgtype])
            return
        
        self._sendToSubscribers(mtype, spname, msg)
コード例 #10
0
ファイル: tmswitch.py プロジェクト: jldupont/pymultiproc
 def doRun(self):
            
     print "TestProcRx.doRun (%s) pid(%s)" % (self.name, os.getpid())
     Bus.publish(self, "log", "doRun: starting (%s) pid(%s)" % (self.name, os.getpid()))
     
     while not self.is_SigTerm() and not self.is_bark() and not self.is_shutdown():
         try:
             Bus.publish(self, "mswitch_pump")
         except Exception,e:
             Bus.publish(self, "log", "*** (%s) Comm Exception: %s" % (self.name, e))
             Bus.publish(self, "shutdown")
             print "getMsg, exception: ", e
             break
         sleep(0.01)
コード例 #11
0
ファイル: process.py プロジェクト: jldupont/pymultiproc
    def run(self):
        """ Called by the multiprocessing module
            once the process is ready to run i.e. after the "fork"
            
            We intercept this call in order to make the
            final preparation before handing the control
            to the user process
        """
        ## For now, there isn't that much to do
        ##  except to reset the process level Message Bus
        ##  to a known start state
        Bus.reset()
        
        Bus.subscribe("_ready", self._hready)

        ## Announce to the Agents we are starting and, incidentally,
        ## that "we" are a "Child" process
        Bus.publish(self, "proc_starting", (self.name, self.pqueue))
        Bus.subscribe("beat",     self._hbeat)
        Bus.subscribe("shutdown", self._hshutdown)
                
        return self.doRun()
コード例 #12
0
ファイル: watchdog.py プロジェクト: jldupont/pymultiproc
    def _hlbeat(self, *p):
        """ Process-local Heart-Beat
        """
        if self._barked:
            return
        
        self._beat_count=self._beat_count+1
        
        if self._beat_count >= self.MISSING_BEATS:
            Bus.publish(self, "%log", "warning", "watchdog expired, proc(%s)" % self.pname)
            Bus.publish(self, "%bark", self.pname)
            self._barked=True 

        Bus.publish(self, "alive", self.pname)
コード例 #13
0
ファイル: process.py プロジェクト: jldupont/pymultiproc
 def __init__(self, name):
     Process.__init__(self)
     self.pqueue=Queue()
     self.name=name
     self.term=False
     self.bark=False
     self._shutdown=False
     
     Bus.subscribe("%bark",    self._hbark)
     Bus.subscribe("_sigterm", self._hsigterm)
     
     ## Publish the proc's details over the local message bus
     ##  The ProcessManager will need those details in order to
     ##  launch the process later on.
     Bus.publish(self, "proc", {"proc":self, "name":name, "queue":self.pqueue})
コード例 #14
0
ファイル: watchdog.py プロジェクト: jldupont/pymultiproc
 def _hbeat(self, _):
     """ Local "beat" message-type
     """
     if self.is_child:
         return
 
     if self._termSent or self._shutdown_initiated:
         return
     
     if self._term:
         Bus.publish(self, "%log", "warning", "SIGTERM received, Main Proc")
         Bus.publish(self, "shutdown")
         self._shutdown_initiated=True
     
     if not self._subToAlive:
         if self._ready:
             self._subToAlive=True
             Bus.subscribe("alive", self._halive)
     
     ### Make sure we have a response from each of the
     ###  Child processes within the Time Interval
     self._beat_count=self._beat_count+1
     if self._beat_count == self.BEATS_THRESHOLD:
         self._beat_count=0
         if len(self.alives) != len(self._procs):
             Bus.publish(self, "%log", "warning", "child process(es) missing")
             Bus.publish(self, "%bark", self.pname)
             Bus.publish(self, "shutdown")
             self._shutdown_initiated
         self.alives=[]
コード例 #15
0
ファイル: watchdog.py プロジェクト: jldupont/pymultiproc
            Bus.publish(self, "_sigterm", self.pname)

    def _hproc_starting(self, (pname, _)):
        self._beat_count=0
        self.is_child=True
        self.pname=pname
        signal.signal(signal.SIGTERM, self._sterm)
        
        ### Only subscribe to the signals
        ###  when we know we are a Child process
        Bus.subscribe("beat",   _cwd._hbeat)        
        Bus.subscribe("%beat",  _cwd._hlbeat)
        

_cwd=Child_WatchDogAgent()
Bus.subscribe("proc_starting", _cwd._hproc_starting)





class Main_WatchDogAgent(object):
    
    BEATS_THRESHOLD = 3
    
    def __init__(self):
        self.pname="__main__"
        self._subToAlive=False
        self._ready=False
        self.alives=[]
        self.is_child=False
コード例 #16
0
ファイル: pman.py プロジェクト: jldupont/pymultiproc
 def _qpname(self):
     """ Answer for the question "pname?"
     """
     Bus.publish(self, "pname", self._name)
コード例 #17
0
ファイル: pman.py プロジェクト: jldupont/pymultiproc
        proc.update(procDetails)
        self._procs[name]=proc
        
    def _hstart(self):
        """ Handler for "start" message
            marking the debut of all registered processes
        """
        for proc_name in self._procs:
            procDetails=self._procs[proc_name]
            try:     proc=procDetails["proc"]
            except:  raise RuntimeError("procDetails require a `proc` entry")
                
            try:
                Bus.publish(self, "%log", "< starting process(%s)" % procDetails["name"])
                proc.start()
                Bus.publish(self, "%log", "> started process(%s)" % procDetails["name"])
            except Exception,e:
                raise RuntimeError("Exception whilst starting process (%s)" % e)
                


## ======================================================================================
## ======================================================================================


_pm=ProcessManager()
Bus.subscribe("start",          _pm._hstart)
Bus.subscribe("proc",           _pm._hproc)
Bus.subscribe("pname?",         _pm._qpname)
Bus.subscribe("proc_starting",  _pm._hproc_starting)
コード例 #18
0
ファイル: tmswitch.py プロジェクト: jldupont/pymultiproc
 def hready(self):
     print "TestProcRx (%s) ready!" % self.name
     self.ready=True
     Bus.subscribe("tick", self._htick)
コード例 #19
0
ファイル: tmswitch.py プロジェクト: jldupont/pymultiproc
    def doRun(self):
        print "TestProc.doRun (%s)" % self.name
        Bus.publish(self, "log", "starting (%s) pid(%s)" % (self.name, os.getpid()))
        try:
            while not self.is_SigTerm() and not self.is_bark() and not self.is_shutdown():
                Bus.publish(self, "mswitch_pump")

                #print "tick (%s)" % self.name
                Bus.publish(self, "tick", self.name, os.getpid())
                sleep(0.150+0.150*random.random())
                
            print ">>> Exiting (%s) <<<" % self.name
            
        except Exception,e:
            Bus.publish(self, "log", "*** (%s) Comm Exception: %s" % (self.name, e))
            print "TestProc: Exiting (%s)" % self.name
            Bus.publish(self, "log", "Exiting (%s)" % e)
            Bus.publish(self, "shutdown")
コード例 #20
0
ファイル: tmswitch.py プロジェクト: jldupont/pymultiproc
sys.path.insert(0, ppkg)

class _Printer(object):
    def __call__(self, msg):
        print "!Bus: ", msg


from pymultiproc.system.mbus import Bus
from pymultiproc.system.process import ProcessClass

from phidgetsdbus.agents import *

Bus.logger=_Printer()
#Bus.debug=True

Bus.publish(None, "logpath", "tmswitch", "~/tmswitch.log")


class TestProc(ProcessClass):
    def __init__(self, name):
        ProcessClass.__init__(self, name)

    def hready(self):
        print "TestProc (%s) ready!" % self.name        

    def doRun(self):
        print "TestProc.doRun (%s)" % self.name
        Bus.publish(self, "log", "starting (%s) pid(%s)" % (self.name, os.getpid()))
        try:
            while not self.is_SigTerm() and not self.is_bark() and not self.is_shutdown():
                Bus.publish(self, "mswitch_pump")
コード例 #21
0
ファイル: logger.py プロジェクト: jldupont/pymultiproc
        self._logger.setLevel(logging.INFO)

    def _hlogpath(self, name, path):
        self._path = path
        self._name = name
        self._setup()

    def _hlog(self, *arg):
        if self._shutdown:
            return

        if self._logger is None:
            self._setup()

        if len(arg) == 1:
            self._logger.log(logging.INFO, arg[0])
        else:
            level = self.mlevel.get(arg[0], logging.INFO)
            self._logger.log(level, arg[1])

    def _hshutdown(self, *arg):
        self._shutdown = True
        self._logger = None
        logging.shutdown([self.fhdlr])


_log = Logger()
Bus.subscribe("%log", _log._hlog)
Bus.subscribe("logpath", _log._hlogpath)
Bus.subscribe("shutdown", _log._hshutdown)
コード例 #22
0
ファイル: mswitch.py プロジェクト: jldupont/pymultiproc
    def _getQueue(self, pname):
        try:    details=self._procs[pname]
        except:
            raise RuntimeError("missing proc from proc list")
        
        try:    q=details["queue"]
        except:
            raise RuntimeError("missing `queue` parameter for proc(%s)" % pname)
        
        return q

## ===============================================================================
## ===============================================================================


_mswitch=MessageSwitch()
        
Bus.subscribe("*",              _mswitch._promiscuousHandler)
Bus.subscribe("_sigterm",       _mswitch._hsigterm)
        
Bus.subscribe("_sub",           _mswitch._hsub)
Bus.subscribe("proc",           _mswitch._hproc)
Bus.subscribe("_ready",         _mswitch._hready)

Bus.subscribe("mswitch_pump",   _mswitch._hpump)
Bus.subscribe("mswitch_params", _mswitch._hparams)

Bus.subscribe("proc_starting",  _mswitch._hproc_starting)

コード例 #23
0
ファイル: heart.py プロジェクト: jldupont/pymultiproc
    def _tick(self):
        self._beat=True ## atomic assignment

    def _qbeat(self):
        if self._beat:
            if self._child:
                Bus.publish(self, "%beat", self.pname)
            else:
                Bus.publish(self, "beat", self.pname)

        self._beat=False ## atomic assignment
        
        
    
_heart=HeartAgent()
Bus.subscribe("%beat?",        _heart._qbeat)
Bus.subscribe("proc_starting", _heart._hproc_starting)





## =====================================================

if __name__=="__main__":
    from time import sleep
    
    Bus.debug=True
    
    class Cb(object):
        def beat(self, state):