RedisServer-Queue 0.0.3__py3-none-any.whl → 0.0.5__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -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`
@@ -0,0 +1,6 @@
1
+ RedisServer_Queue/RedisServer.py,sha256=HhuGiyLnDzKSMu7EwI56zPRYJAncufGmsbD0JjYqORw,2883
2
+ RedisServer_Queue-0.0.5.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
3
+ RedisServer_Queue-0.0.5.dist-info/METADATA,sha256=QzFR9MMW_lKJdHft1vXiEJhiE2ZQGKNpgZ3W_CffIRk,995
4
+ RedisServer_Queue-0.0.5.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
5
+ RedisServer_Queue-0.0.5.dist-info/top_level.txt,sha256=rcsBD4X_qh4CYFrIE79f5ETrrVBFWcHmqrSydQVyDHI,18
6
+ RedisServer_Queue-0.0.5.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.43.0)
2
+ Generator: setuptools (75.8.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,6 +0,0 @@
1
- RedisServer_Queue/RedisServer.py,sha256=xua8R_elXjfSw-izd6UWj7xqogvw0YriP5ugN04Q-Eg,3233
2
- RedisServer_Queue-0.0.3.dist-info/LICENSE,sha256=6kbiFSfobTZ7beWiKnHpN902HgBx-Jzgcme0SvKqhKY,1091
3
- RedisServer_Queue-0.0.3.dist-info/METADATA,sha256=525YnTUOGmGefTqLW3qdZtir2Zjm1kii3eYUs3CH7n4,598
4
- RedisServer_Queue-0.0.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
5
- RedisServer_Queue-0.0.3.dist-info/top_level.txt,sha256=rcsBD4X_qh4CYFrIE79f5ETrrVBFWcHmqrSydQVyDHI,18
6
- RedisServer_Queue-0.0.3.dist-info/RECORD,,