yzk_wechat_event/apps/msg/sub.py

26 lines
607 B
Python
Raw Normal View History

2023-12-21 14:46:34 +08:00
import time
from redis import StrictRedis
import json
rc = StrictRedis(host='localhost', port=6379, db=0)
ps = rc.pubsub()
2023-12-21 17:17:07 +08:00
ps.subscribe('jqr:msg')
2023-12-21 14:46:34 +08:00
msg_list = []
2023-12-21 17:17:07 +08:00
batch_size = 2
2023-12-21 14:46:34 +08:00
while True:
for item in ps.listen():
if item['type'] == 'message':
data = item.get('data')
print(data)
msg_list.append(data)
2023-12-21 17:17:07 +08:00
if len(msg_list) >= batch_size:
2023-12-21 14:46:34 +08:00
try:
time.sleep(1)
2023-12-21 17:17:07 +08:00
msg_list = msg_list[-batch_size:]
2023-12-21 14:46:34 +08:00
except Exception as e:
print(e)
print(len(msg_list))