RedisServer-Queue 0.0.3__tar.gz → 0.0.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: RedisServer-Queue
3
- Version: 0.0.3
3
+ Version: 0.0.5
4
4
  Summary: RedisServer-Queue is a simple redis server
5
5
  Author-email: NelsonLongXiang <1169207670@qq.com>
6
6
  Project-URL: Homepage, https://github.com/NelsonLongxiang/RedisServer
@@ -13,3 +13,26 @@ Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
14
 
15
15
  RedisServer-Queue is a simple redis server
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ pip install -i https://pypi.org/simple RedisServer-Queue
21
+ ```
22
+
23
+ ```commandline
24
+ pip install --upgrade -i https://pypi.org/simple RedisServer-Queue
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ #### 请参考test目录下test.py文件
30
+
31
+ ## License
32
+
33
+ [MIT](https://choosealicense.com/licenses/mit/)
34
+
35
+ ### 作者联系方式
36
+
37
+ 1. 作者邮箱:`1169207670@qq.com`
38
+ 2. Wechat: `1169207670`
@@ -0,0 +1,24 @@
1
+ RedisServer-Queue is a simple redis server
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ pip install -i https://pypi.org/simple RedisServer-Queue
7
+ ```
8
+
9
+ ```commandline
10
+ pip install --upgrade -i https://pypi.org/simple RedisServer-Queue
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ #### 请参考test目录下test.py文件
16
+
17
+ ## License
18
+
19
+ [MIT](https://choosealicense.com/licenses/mit/)
20
+
21
+ ### 作者联系方式
22
+
23
+ 1. 作者邮箱:`1169207670@qq.com`
24
+ 2. Wechat: `1169207670`
@@ -9,52 +9,30 @@ import os
9
9
  import sys
10
10
  import time
11
11
  import uuid
12
-
13
12
  import redis
14
13
 
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
23
-
24
-
25
- def init(rids_boj=None):
26
- '''
27
- 初始化redis
28
- :param rids_boj:
29
- :return:
30
- '''
31
- global r
32
- if rids_boj:
33
- r = rids_boj
34
- else:
35
- r = r(host='localhost', port=6379, db=0)
36
-
37
14
 
38
15
  class RedisQueue:
39
- def __init__(self, topic=None):
16
+ def __init__(self, r: redis.Redis, topic: str = 'test_queue'):
40
17
  self.topic = topic
18
+ self.r = r
41
19
 
42
20
  def get(self):
43
21
  # 弹出指定数量的数据
44
- data = r.rpop(self.topic)
22
+ data = self.r.rpop(self.topic)
45
23
  return data if data else None
46
24
 
47
25
  def put(self, value):
48
26
  if isinstance(value, list):
49
- r.lpush(self.topic, *value)
27
+ self.r.lpush(self.topic, *value)
50
28
  else:
51
- r.lpush(self.topic, *[value])
29
+ self.r.lpush(self.topic, *[value])
52
30
 
53
31
  def clear(self):
54
- r.delete(self.topic)
32
+ self.r.delete(self.topic)
55
33
 
56
34
  def size(self):
57
- return r.llen(self.topic)
35
+ return self.r.llen(self.topic)
58
36
 
59
37
  def qsize(self):
60
38
  return self.size()
@@ -63,29 +41,29 @@ class RedisQueue:
63
41
  return [self.get() for _ in range(num)]
64
42
 
65
43
  def re_data(self):
66
- ch_keys = r.keys(f"ack_{self.topic}_*")
44
+ ch_keys = self.r.keys(f"ack_{self.topic}_*")
67
45
  for key in ch_keys:
68
- data = r.get(key)
46
+ data = self.r.get(key)
69
47
  if data:
70
- q = RedisQueue(self.topic)
48
+ q = RedisQueue(self.r, self.topic)
71
49
  t, _data = json.loads(data)
72
- r.delete(key)
50
+ self.r.delete(key)
73
51
  q.put(_data)
74
52
  return len(ch_keys)
75
53
 
76
54
  def get_all(self):
77
- return r.lrange(self.topic, 0, -1)
55
+ return self.r.lrange(self.topic, 0, -1)
78
56
 
79
57
 
80
58
  class RedisMQ:
81
59
  def __init__(self):
82
60
  self.switch = 1
83
61
 
84
- def start_receive(self, topic, callback, count=-1):
62
+ def start_receive(self, r, topic, callback, count=-1):
85
63
  while self.switch:
86
64
  data = r.rpop(topic)
87
65
  if data:
88
- ch = RedisCh(topic, data)
66
+ ch = RedisCh(r, topic, data)
89
67
  callback(ch, data)
90
68
  if count == 1:
91
69
  return
@@ -94,7 +72,7 @@ class RedisMQ:
94
72
  for key in ch_keys:
95
73
  data = r.get(key)
96
74
  if data:
97
- q = RedisQueue(topic)
75
+ q = RedisQueue(r, topic)
98
76
  t, _data = json.loads(data)
99
77
  if time.time() - t > 10 * 60:
100
78
  r.delete(key)
@@ -109,17 +87,19 @@ class RedisMQ:
109
87
 
110
88
 
111
89
  class RedisCh:
112
- def __init__(self, topic, data):
90
+ def __init__(self, r, topic, data):
91
+ self.r = r
113
92
  self.topic = topic
114
93
  self.id = uuid.uuid4()
115
94
  r.set(f"ack_{topic}_{self.id}", json.dumps([time.time(), data.decode()]))
116
95
 
117
96
  def basic_ack(self):
118
- r.delete(f"ack_{self.topic}_{self.id}")
97
+ self.r.delete(f"ack_{self.topic}_{self.id}")
119
98
 
120
99
 
121
100
  if __name__ == '__main__':
122
- r.delete('test')
101
+ r1 = redis.Redis()
102
+ r1.delete('test')
123
103
  # a = RedisQueue("test")
124
104
  # a.put(1)
125
105
  # print(type(a.get()))
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: RedisServer-Queue
3
- Version: 0.0.3
3
+ Version: 0.0.5
4
4
  Summary: RedisServer-Queue is a simple redis server
5
5
  Author-email: NelsonLongXiang <1169207670@qq.com>
6
6
  Project-URL: Homepage, https://github.com/NelsonLongxiang/RedisServer
@@ -13,3 +13,26 @@ Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
14
 
15
15
  RedisServer-Queue is a simple redis server
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ pip install -i https://pypi.org/simple RedisServer-Queue
21
+ ```
22
+
23
+ ```commandline
24
+ pip install --upgrade -i https://pypi.org/simple RedisServer-Queue
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ #### 请参考test目录下test.py文件
30
+
31
+ ## License
32
+
33
+ [MIT](https://choosealicense.com/licenses/mit/)
34
+
35
+ ### 作者联系方式
36
+
37
+ 1. 作者邮箱:`1169207670@qq.com`
38
+ 2. Wechat: `1169207670`
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.0"]
3
3
  build-backend = "setuptools.build_meta"
4
4
  [project]
5
5
  name = "RedisServer-Queue"
6
- version = "0.0.3"
6
+ version = "0.0.5"
7
7
  authors = [
8
8
  { name = "NelsonLongXiang", email = "1169207670@qq.com" },
9
9
  ]
@@ -0,0 +1,84 @@
1
+ #!/user/bin/env python3
2
+ # -*- coding: UTF-8 -*-
3
+ # @Time : 2024/6/15 下午5:02
4
+ # @Author : 龙翔
5
+ # @File :test.py.py
6
+ # @Software: PyCharm
7
+
8
+ import os
9
+ import sys
10
+
11
+ import redis
12
+
13
+ # 将当前文件夹添加到环境变量
14
+ if os.path.basename(__file__) in ['run.py', 'main.py', '__main__.py']:
15
+ if '.py' in __file__:
16
+ sys.path.append(os.path.abspath(os.path.dirname(__file__)))
17
+ else:
18
+ sys.path.append(os.path.abspath(__file__))
19
+
20
+ # 导入模块
21
+ from RedisServer_Queue.RedisServer import RedisQueue, RedisMQ
22
+
23
+ # 初始化Redis服务
24
+ # 默认使用本地Redis服务 6379端口 如果您使用的是远程Redis服务 请修改将实例化后的r对象传入RedisServer.init(r)
25
+ # 使用本地Redis服务端口是6379 默认db是0 则不需要传入参数
26
+ '''
27
+ r = redis.Redis(host='localhost', port=6379, db=0)
28
+ '''
29
+ r = redis.Redis(host='localhost', port=6379, db=0)
30
+
31
+
32
+ def hello_world():
33
+ '''
34
+ get 数据返回类型为bytes类型
35
+ :return:
36
+ '''
37
+ msg_queue = RedisQueue(r, topic='msg_queue')
38
+ if msg_queue.qsize() == 0:
39
+ msg_queue.put('hello world')
40
+
41
+ print(msg_queue.get())
42
+
43
+
44
+ def ack_test():
45
+ '''
46
+ ack_test
47
+ :return:
48
+ '''
49
+ msg_queue = RedisQueue(r, topic='msg_queue')
50
+ print("重置数据,初始化时运行,返回值为重置数据量!", msg_queue.re_data())
51
+ msg_queue.put('hello world')
52
+
53
+ class ThreadAck(RedisQueue, RedisMQ):
54
+ def __init__(self, topic):
55
+ RedisQueue.__init__(self, r, topic)
56
+ RedisMQ.__init__(self)
57
+ self.ch = None
58
+
59
+ def run(self):
60
+ '''count 默认为-1;当count=1时,表示只获取一个数据,当count>1时,表示获取count个数据
61
+ 根据需要自行修改
62
+ '''
63
+ self.start_receive(self.r, topic=self.topic, callback=self.callback)
64
+
65
+ def callback(self, ch, body):
66
+ self.ch = ch
67
+ self.work(body)
68
+ print("剩余队列长度:", self.qsize())
69
+
70
+ def work(self, data):
71
+ if data:
72
+ print(data)
73
+ '''
74
+ ack 注释 可以通过re_data()方法重新将数据放入队列
75
+ '''
76
+ self.ch.basic_ack()
77
+
78
+ thread_ack = ThreadAck(topic='msg_queue')
79
+ thread_ack.run()
80
+
81
+
82
+ if __name__ == '__main__':
83
+ # hello_world()
84
+ ack_test()
@@ -1 +0,0 @@
1
- RedisServer-Queue is a simple redis server
@@ -1,19 +0,0 @@
1
- #!/user/bin/env python3
2
- # -*- coding: UTF-8 -*-
3
- # @Time : 2024/6/15 下午5:02
4
- # @Author : 龙翔
5
- # @File :test.py.py
6
- # @Software: PyCharm
7
-
8
- import os
9
- import sys
10
-
11
- # 将当前文件夹添加到环境变量
12
- if os.path.basename(__file__) in ['run.py', 'main.py', '__main__.py']:
13
- if '.py' in __file__:
14
- sys.path.append(os.path.abspath(os.path.dirname(__file__)))
15
- else:
16
- sys.path.append(os.path.abspath(__file__))
17
- from RedisServer_Queue import RedisServer
18
-
19
- RedisServer.init()