Redis儲存結構之列表(List)
阿新 • • 發佈:2018-11-24
訊息佇列
Redis列表只是字串列表,按插入順序排序。您可以向Redis列表的頭部或尾部新增元素。
常用的方法有:lpush、rpush、lpop、rpop、blpop、brpop、lrange
BLPOP
BRPOP
BRPOPLPUSH
LINDEX
LINSERT
LLEN
LPOP
LPUSH
LPUSHX
LRANGE
LREM
LSET
LTRIM
RPOP
RPOPLPUSH
RPUSH
RPUSHX
可使用“blpop”實現訊息佇列,如下:
import _thread import logging import random import time import redis client = redis.Redis(host='10.182.45.93', port=6379, db=0, decode_responses=True) topic = 'push-and-pop' def pop(): while True: _resp = client.blpop(topic) logging.info(_resp[1]) def push_and_pop(): _thread.start_new_thread(pop, ()) for i in range(0, 100): n = random.random() * 1.5 time.sleep(n) logging.info('No.%s: %s', i, n) client.rpush(topic, n) if __name__ == '__main__': push_and_pop()