-
Notifications
You must be signed in to change notification settings - Fork 2
/
redis_tests.py
62 lines (46 loc) · 1.36 KB
/
redis_tests.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from threading import Thread
import time
import redis
class RedisPublisher(Thread):
def __init__(self) -> None:
super().__init__()
self.redis = redis.Redis("127.0.0.1", 6379, 0)
def run(self) -> None:
i = 0
while True:
print(f"Publishing: {i}")
# self.redis.set("__test__", i)
self.redis.publish("__test__", i)
i += 1
time.sleep(3)
class RedisSubscriber(Thread):
def __init__(self) -> None:
super().__init__()
self.redis = redis.Redis(
"127.0.0.1", 6379, 0, decode_responses=True, charset="utf-8"
)
def get_message(self, timeout):
val = None
msg = self.sub.get_message(timeout=timeout)
while msg is not None:
val = msg
msg = self.sub.get_message(timeout=timeout)
return val
def run(self) -> None:
print("Creating subscribing")
self.sub = self.redis.pubsub()
self.sub.subscribe("__test__")
while True:
val = self.get_message(timeout=0.1)
if val is not None:
print("Got", val["data"])
else:
print("Got None")
time.sleep(0.1)
def main():
RedisPublisher().start()
RedisSubscriber().start()
while True:
pass
if __name__ == "__main__":
main()