RedisServer-Queue 0.0.1__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,115 @@
1
+ #!/user/bin/env python3
2
+ # -*- coding: UTF-8 -*-
3
+ # @Time : 2024/6/6 上午6:16
4
+ # @Author : 龙翔
5
+ # @File :RedisServer.py
6
+ # @Software: PyCharm
7
+ import json
8
+ import os
9
+ import sys
10
+ import time
11
+ import uuid
12
+
13
+ import redis
14
+
15
+ # 将当前文件夹添加到环境变量
16
+ if os.path.basename(__file__) in ['run.py', 'main.py', '__main__.py']:
17
+ if '.py' in __file__:
18
+ sys.path.append(os.path.abspath(os.path.dirname(__file__)))
19
+ else:
20
+ sys.path.append(os.path.abspath(__file__))
21
+
22
+ r = redis.Redis(host='localhost', port=6379, db=0)
23
+
24
+
25
+ class RedisQueue:
26
+ def __init__(self, topic=None):
27
+ self.topic = topic
28
+
29
+ def get(self):
30
+ # 弹出指定数量的数据
31
+ data = r.rpop(self.topic)
32
+ return data if data else None
33
+
34
+ def put(self, value):
35
+ if isinstance(value, list):
36
+ r.lpush(self.topic, *value)
37
+ else:
38
+ r.lpush(self.topic, *[value])
39
+
40
+ def clear(self):
41
+ r.delete(self.topic)
42
+
43
+ def size(self):
44
+ return r.llen(self.topic)
45
+
46
+ def qsize(self):
47
+ return self.size()
48
+
49
+ def get_mul(self, num):
50
+ return [self.get() for _ in range(num)]
51
+
52
+ def re_data(self):
53
+ ch_keys = r.keys(f"ack_{self.topic}_*")
54
+ for key in ch_keys:
55
+ data = r.get(key)
56
+ if data:
57
+ q = RedisQueue(self.topic)
58
+ t, _data = json.loads(data)
59
+ r.delete(key)
60
+ q.put(_data)
61
+ return len(ch_keys)
62
+
63
+ def get_all(self):
64
+ return r.lrange(self.topic, 0, -1)
65
+
66
+
67
+ class RedisMQ:
68
+ def __init__(self):
69
+ self.switch = 1
70
+
71
+ def start_receive(self, topic, callback, count=-1):
72
+ while self.switch:
73
+ data = r.rpop(topic)
74
+ if data:
75
+ ch = RedisCh(topic, data)
76
+ callback(ch, data)
77
+ if count == 1:
78
+ return
79
+ continue
80
+ ch_keys = r.keys(f"ack_{topic}_*")
81
+ for key in ch_keys:
82
+ data = r.get(key)
83
+ if data:
84
+ q = RedisQueue(topic)
85
+ t, _data = json.loads(data)
86
+ if time.time() - t > 10 * 60:
87
+ r.delete(key)
88
+ q.put(_data)
89
+ del q
90
+ if len(ch_keys) == 0:
91
+ time.sleep(10)
92
+ time.sleep(1)
93
+
94
+ def stop(self):
95
+ self.switch = 0
96
+
97
+
98
+ class RedisCh:
99
+ def __init__(self, topic, data):
100
+ self.topic = topic
101
+ self.id = uuid.uuid4()
102
+ r.set(f"ack_{topic}_{self.id}", json.dumps([time.time(), data.decode()]))
103
+
104
+ def basic_ack(self):
105
+ r.delete(f"ack_{self.topic}_{self.id}")
106
+
107
+
108
+ if __name__ == '__main__':
109
+ r.delete('test')
110
+ # a = RedisQueue("test")
111
+ # a.put(1)
112
+ # print(type(a.get()))
113
+ # r.set('test', 1)
114
+ # print(r.get('test'), type(r.get('test').decode('utf8')))
115
+ # RedisQueue.put(value=1, topic="test")
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 The Python Packaging Authority
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,14 @@
1
+ Metadata-Version: 2.1
2
+ Name: RedisServer-Queue
3
+ Version: 0.0.1
4
+ Summary: RedisServer-Queue is a simple redis server
5
+ Author-email: NelsonLongXiang <1169207670@qq.com>
6
+ Project-URL: Homepage, https://github.com/NelsonLongxiang/RedisServer
7
+ Project-URL: Issues, https://github.com/NelsonLongxiang/RedisServer/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+
@@ -0,0 +1,6 @@
1
+ RedisServer/RedisServer.py,sha256=-RwVSFKx5sN7XCZcJpip8PeWCmoCmr_VHg5ApUW-1zI,3049
2
+ RedisServer_Queue-0.0.1.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
3
+ RedisServer_Queue-0.0.1.dist-info/METADATA,sha256=XqxOyPvjUtW5lkEWBHWn504leIrfSiIylAJvB6iY4eA,554
4
+ RedisServer_Queue-0.0.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
5
+ RedisServer_Queue-0.0.1.dist-info/top_level.txt,sha256=KqY5fDKu1qjJQn9S9SA9hyn4IkuL07UHTsc45MmCB58,12
6
+ RedisServer_Queue-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.43.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ RedisServer