def custom_dispatch(source, q, pq, dispatcher, low_priority_burst_size=5): """ For customer dispatching @param source: identifier for 'source' @param low_priority_burst_size: integer, maximum size of normal queue processing at a time @param q: normal queue @param pq: priority queue @param dispatcher: callable """ while True: try: envelope = pq.get(False) orig, mtype, payload = envelope pargs, _kargs = payload ## skip self if orig == source: continue handled, snooping = dispatcher(mtype, *pargs) if handled == False: ## low overhead - once per run print "* '%s': not interest in '%s' message type" % (orig, mtype) mswitch.publish(source, "__interest__", source, mtype, False, snooping, q, pq) break except KeyboardInterrupt: raise except Empty: break continue burst = low_priority_burst_size while True: try: envelope = q.get(False) orig, mtype, payload = envelope pargs, _kargs = payload ## skip self if orig == source: continue handled, snooping = dispatcher(mtype, *pargs) if handled == False: ## low overhead - once per run print "* '%s': not interest in '%s' message type" % (orig, mtype) mswitch.publish(source, "__interest__", source, mtype, False, snooping, q, pq) burst -= 1 if burst == 0: break except KeyboardInterrupt: raise except Empty: break continue
def _process(self, envelope): mtype, _payload = envelope #if mtype!="tick": # print "base._process: mtype: " + str(mtype) interested=self.mmap.get(mtype, None) if interested==False: return False quit, _mtype, handled=mdispatch(self, self.id, envelope) if quit: shutdown_handler=getattr(self, "h_shutdown", None) if shutdown_handler is not None: shutdown_handler() if handled is not None: self.mmap[mtype]=handled ### signal this Agent's interest status (True/False) ### to the central message switch if interested is None: if mtype!="__quit__": mswitch.publish(self.__class__, "__interest__", (mtype, handled, self.iq)) ### This debug info is extermely low overhead... keep it. if interested is None and handled: self.dprint("Agent(%s) interested(%s)" % (self.__class__, mtype)) self.dprint("Agent(%s) interests: %s" % (self.__class__, self.mmap)) return quit
def pub(self, msgType, msg=None, *pargs, **kargs): """ Publish method Used by Agents to publish a message through the 'mswitch' (message switch) to other interested Agents """ mswitch.publish(self.id, msgType, msg, *pargs, **kargs)
def custom_dispatch(source, q, pq, dispatcher, low_priority_burst_size=5): """ For customer dispatching @param source: identifier for 'source' @param low_priority_burst_size: integer, maximum size of normal queue processing at a time @param q: normal queue @param pq: priority queue @param dispatcher: callable """ while True: try: envelope=pq.get(False) orig, mtype, payload = envelope pargs, _kargs = payload ## skip self if orig==source: continue handled, snooping=dispatcher(mtype, *pargs) if handled==False: ## low overhead - once per run print "* '%s': not interest in '%s' message type" % (orig, mtype) mswitch.publish(source, "__interest__", source, mtype, False, snooping, q, pq) break except KeyboardInterrupt: raise except Empty: break continue burst=low_priority_burst_size while True: try: envelope=q.get(False) orig, mtype, payload = envelope pargs, _kargs = payload ## skip self if orig==source: continue handled, snooping=dispatcher(mtype, *pargs) if handled==False: ## low overhead - once per run print "* '%s': not interest in '%s' message type" % (orig, mtype) mswitch.publish(source, "__interest__", source, mtype, False, snooping, q, pq) burst -= 1 if burst == 0: break except KeyboardInterrupt: raise except Empty: break continue
def dispatcher(source, q, pq, low_priority_burst_size=5): """ Message dispatcher """ while True: try: envelope = pq.get(False) orig, mtype, payload = envelope pargs, _kargs = payload ## skip self if orig == source: continue handled, snooping = dispatcher(mtype, *pargs) if handled == False: ## low overhead - once per run print "* '%s': not interest in '%s' message type" % (orig, mtype) mswitch.publish(source, "__interest__", source, mtype, False, snooping, pq) break except KeyboardInterrupt: raise except Empty: break continue burst = low_priority_burst_size while True: try: envelope = q.get(False) orig, mtype, payload = envelope pargs, _kargs = payload ## skip self if orig == source: continue handled, snooping = dispatcher(mtype, *pargs) if handled == False: ## low overhead - once per run print "* '%s': not interest in '%s' message type" % (orig, mtype) mswitch.publish(source, "__interest__", source, mtype, False, snooping, q) burst -= 1 if burst == 0: break except KeyboardInterrupt: raise except Empty: break continue
def dispatcher(source, q, pq, low_priority_burst_size=5): """ Message dispatcher """ while True: try: envelope=pq.get(False) orig, mtype, payload = envelope pargs, _kargs = payload ## skip self if orig==source: continue handled, snooping=dispatcher(mtype, *pargs) if handled==False: ## low overhead - once per run print "* '%s': not interest in '%s' message type" % (orig, mtype) mswitch.publish(source, "__interest__", source, mtype, False, snooping, pq) break except KeyboardInterrupt: raise except Empty: break continue burst=low_priority_burst_size while True: try: envelope=q.get(False) orig, mtype, payload = envelope pargs, _kargs = payload ## skip self if orig==source: continue handled, snooping=dispatcher(mtype, *pargs) if handled==False: ## low overhead - once per run print "* '%s': not interest in '%s' message type" % (orig, mtype) mswitch.publish(source, "__interest__", source, mtype, False, snooping, q) burst -= 1 if burst == 0: break except KeyboardInterrupt: raise except Empty: break continue
def _process(self, envelope): orig, mtype, _payload = envelope #if mtype!="tick": # print "base._process: mtype: " + str(mtype) interested=self.mmap.get(mtype, None) if interested==False: return False quit, _mtype, handled, snooping=mdispatch(self, self.id, envelope) if quit: shutdown_handler=getattr(self, "h_shutdown", None) if shutdown_handler is not None: shutdown_handler() ## not much more to do here... ## but shouldn't happen anyhow!!! #paranoia... if handled is None: return interestFirstTimeNoted=self.mmap.get(mtype, None) #if interestFirstTimeNoted is None: # print "Agent(%s) mtype(%s) interested(%s) snooping(%s)" % (self.__class__, mtype, handled, snooping) # print "Agent(%s) map: %s" % (self.__class__, self.mmap) ### signal this Agent's interest status (True/False) ### to the central message switch if interested is None: if mtype!="__quit__": if mtype not in self.responsesInterest: mswitch.publish(self.id, "__interest__", self.agent_name, mtype, handled, snooping, self.iq, self.isq) self.responsesInterest.append(mtype) ## stop sending to self definitely if handled is None: handled=False print "+++ interest msg-orig(%s) target(%s) mtype(%s): (%s)" % (orig, self.agent_name, mtype, handled) self.mmap[mtype]=handled ### This debug info is extermely low overhead... keep it. #if interested is None and handled: # print "Agent(%s) interested(%s) snooping(%s)" % (self.__class__, mtype, snooping) # print "Agent(%s) map: %s" % (self.__class__, self.mmap) return quit
def message_processor(src_agent, agent_name, agent_id, interest_map, responsesInterestList, iq, isq, envelope): """ Processes 1 message envelope Used in conjunction with 'process_queues' """ orig, mtype, _payload = envelope interested = interest_map.get(mtype, None) if interested == False: return False quit, _mtype, handled, snooping = mdispatch(src_agent, agent_id, envelope) if quit: shutdown_handler = getattr(src_agent, "h_shutdown", None) if shutdown_handler is not None: shutdown_handler() return True ## not much more to do here... ## but shouldn't happen anyhow!!! #paranoia... if handled is None: return if interested is None: if mtype != "__quit__": if mtype not in responsesInterestList: mswitch.publish(agent_id, "__interest__", agent_name, agent_id, mtype, handled, snooping, iq, isq) responsesInterestList.append(mtype) ## stop sending to self definitely if handled is None: handled = False if debug_interest: print "+++ interest msg-orig(%s) target(%s) mtype(%s): (%s)" % ( orig, agent_name, mtype, handled) interest_map[mtype] = handled return quit
def message_processor(src_agent, agent_name, agent_id, interest_map, responsesInterestList, iq, isq, envelope): """ Processes 1 message envelope Used in conjunction with 'process_queues' """ orig, mtype, _payload = envelope interested=interest_map.get(mtype, None) if interested==False: return False quit, _mtype, handled, snooping=mdispatch(src_agent, agent_id, envelope) if quit: shutdown_handler=getattr(src_agent, "h_shutdown", None) if shutdown_handler is not None: shutdown_handler() return True ## not much more to do here... ## but shouldn't happen anyhow!!! #paranoia... if handled is None: return if interested is None: if mtype!="__quit__": if mtype not in responsesInterestList: mswitch.publish(agent_id, "__interest__", agent_name, agent_id, mtype, handled, snooping, iq, isq) responsesInterestList.append(mtype) ## stop sending to self definitely if handled is None: handled=False if debug_interest: print "+++ interest msg-orig(%s) target(%s) mtype(%s): (%s)" % (orig, agent_name, mtype, handled) interest_map[mtype]=handled return quit
def _pub(self, msgType, *pargs, **kargs): """ Message Publication facility """ mswitch.publish(self.id, msgType, *pargs, **kargs)
def pub(self, msgType, *pargs, **kargs): """ Message Publication facility """ if not self.halting: mswitch.publish(self.id, msgType, *pargs, **kargs)
def pub(self, msgType, *pargs): mswitch.publish(self.id, msgType, *pargs)
def h_mtype1(self, *pargs, **kargs): print "Agent1.h_mtype1: pargs: %s kargs: %s" % (pargs, kargs) def h_mtype2(self, *pargs, **kargs): print "Agent1.h_mtype2: pargs: %s kargs: %s" % (pargs, kargs) class Agent2(AgentThreadedBase): def __init__(self): AgentThreadedBase.__init__(self) def h_mtype1(self, *pargs, **kargs): print "Agent2.h_mtype1: pargs: %s kargs: %s" % (pargs, kargs) self.pub("mtype2", "mtype2 message!") def h_mtype2(self, *pargs, **kargs): print "Agent2.h_mtype2: pargs: %s kargs: %s" % (pargs, kargs) a1=Agent1() a2=Agent2() a1.start() a2.start() mswitch.publish(None, "mtype1", "coucou!") mswitch.publish(None, "mtype1", "coucou2!") while True: pass