欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python连接redis实现有序队列queue先进先出/后进先出

程序员文章站 2022-03-01 18:09:44
...

目的:实现有序队列queue;先进先出/后进先出。

贴代码:

from rediscluster import StrictRedisCluster


def redis_queue():
    redis_nodes = [
        {'host': '192.168.30.153', 'port': 6383},
        {'host': '192.168.30.153', 'port': 6384},
    ]

    try:
        redis_conn = StrictRedisCluster(startup_nodes=redis_nodes)
        key = 'py_queue'
        redis_conn.lpush(key, '1')
        redis_conn.lpush(key, 'https://www.baidu.com/?tn=50000049_hao_pg')
        result1 = redis_conn.rpop(key)
        result2 = redis_conn.rpop(key)
        result3 = redis_conn.rpop(key)

        print("result1:", result1)
        print("result2:", result2)
        print("result3:", result3)

        result4 = result1.decode('utf-8')
        print(result4 == '1')
        print(result4)

        redis_conn.expire(key, 10000)

    except Exception as e:
        print('error:', e)


redis_queue()