MDU:html
import myQueue from myThread import myThread class mdu: def __init__(self, mduID): self.mduId = mduID self.msgQue = myQueue.myQueue(10) self.map = {} def getMduID(self): return self.mduId def registPid(self, pidID, pid): self.map[pidID] = pid if 1==len(self.map): self.run() def msgEnQueue(self,msg): self.msgQue.enQueue(msg) def msgProcess(self): while True: msg = self.msgQue.deQueue() recvPid = msg.getRecvPid() self.map[recvPid].msgProcess(msg); def run(self): t = myThread(self.msgProcess) t.start()
PID:數據結構
import message import support import mdu import pdb class pid: def __init__(self, pidID): self.pidID = pidID self.registMe() def registMe(self): support.registPid(self) def getPidID(self): return self.pidID
SUPPORT:架構
import mdu import pdb mduMap = {} def registMdu(mdu): mduMap[mdu.getMduID()] = mdu def getMdu(revPid): return mduMap[revPid&0xFFFF0000>>16] def registPid(pid): mdu = getMdu(pid.getPidID()) #pdb.set_trace() mdu.registPid(pid.getPidID(), pid) def sendMsg(msg): mdu = getMdu(msg.getRecvPid()) mdu.msgEnQueue(msg)
MQ:併發
from threading import Lock from threading import Condition import threading class myQueue: def __init__(self, size): self.size = size self.list = list() self.lock = Lock() self.notFullCond = Condition(self.lock) self.notEmptyCond = Condition(self.lock) def isFull(self): if self.size == len(self.list): return True return False def isEmpty(self): if 0 == len(self.list): return True return False def enQueue(self, elem): self.lock.acquire() while self.isFull(): print('queue is full, waiting...') self.notFullCond.wait() print(threading.current_thread().getName() + ' product ' + str(elem)) self.list.append(elem) self.notEmptyCond.notify() self.lock.release() def deQueue(self): self.lock.acquire() while self.isEmpty(): print('queue is empty, waiting...') self.notEmptyCond.wait() elem = self.list[0] del(self.list[0]) print(threading.current_thread().getName() + ' consume ' + str(elem)) self.notFullCond.notify_all() self.lock.release() return elem
轉載請註明原始出處:http://www.cnblogs.com/chencheng/p/3236158.htmlapp