1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import queue
class MessageQueue(): def __init__(self, qMapSz=1, qPlayerMoveSz=100, qUtilitySz=10, q2ServerMsgSz=10, q2WebMsgSz=5): ''' qMap: [mapname] qPlayersMove: [[posX, posY, name, steam3id, clientid]] qUtility: [utid, uttype, posX, posY] q2WebMsg: [ip/name, msg] q2ServerMsg: [ip, msg] ''' self.qMsg = { "qMap": queue.Queue(maxsize=qMapSz), "qPlayersMove": queue.Queue(maxsize=qPlayerMoveSz), "qUtility": queue.Queue(maxsize=qUtilitySz), "q2WebMsg": queue.Queue(maxsize=q2WebMsgSz), "q2ServerMsg": queue.Queue(maxsize=q2ServerMsgSz), } def qPut(self, qName: str, value: list=[]): try: if self.qMsg[qName].full(): self.qMsg[qName].get_nowait() self.qMsg[qName].put_nowait(value) except: raise Exception("Queue({}) Put Error!".format(qName)) def qGetAllMsg_noWait(self, qExcept=[]): allMsg = {} for key, value in self.qMsg.items(): if key in qExcept: continue allMsg[key] = [] if value.empty() else value.get_nowait() return allMsg
def qGet_noWait(self, qName: str): try: return (False, []) if self.qMsg[qName].empty() else (True, self.qMsg[qName].get_nowait()) except: raise Exception("Queue({}) Get Error!".format(qName))
def qClearAll(self): for key in self.qMsg.keys(): self.qMsg[key] = queue.Queue()
|