MCPcopy Create free account
hub / github.com/InternLM/HuixiangDou / Queue

Class Queue

web/proxy/test.py:30–82  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29
30class Queue:
31
32 def __init__(self, name, namespace='HuixiangDou', **redis_kwargs):
33 self.__db = redis.Redis(host=redis_host(),
34 port=redis_port(),
35 password=redis_passwd(),
36 charset='utf-8',
37 decode_responses=True)
38 self.key = '%s:%s' % (namespace, name)
39
40 def qsize(self):
41 """Return the approximate size of the queue."""
42 return self.__db.llen(self.key)
43
44 def empty(self):
45 """Return True if the queue is empty, False otherwise."""
46 return self.qsize() == 0
47
48 def put(self, item):
49 """Put item into the queue."""
50 self.__db.rpush(self.key, item)
51
52 def peek_tail(self):
53 return self.__db.lrange(self.key, -1, -1)
54
55 def get(self, block=True, timeout=None):
56 """Remove and return an item from the queue.
57
58 If optional args block is true and timeout is None (the default), block
59 if necessary until an item is available.
60 """
61 if block:
62 item = self.__db.blpop(self.key, timeout=timeout)
63 else:
64 item = self.__db.lpop(self.key)
65
66 if item:
67 item = item[1]
68 return item
69
70 def get_all(self):
71 """Get add messages in queue without block."""
72 ret = []
73 while True:
74 item = self.__db.lpop(self.key)
75 if not item:
76 break
77 ret.append(item)
78 return ret
79
80 def get_nowait(self):
81 """Equivalent to get(False)."""
82 return self.get(False)
83
84
85class TaskCode(Enum):

Callers 1

test.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected