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