python实现与redis交互操作详解

作者:随风行云 时间:2022-07-07 17:37:18 

本文实例讲述了python实现与redis交互操作。分享给大家供大家参考,具体如下:

相关内容:

  • redis模块的使用

    • 设置值

    • 获取值

    • 安装模块

    • 导入模块

    • 连接方式

    • 连接池

    • 操作

    • 管道

    • 事务

    • 订阅\发布

     首发时间:2018-03-14 15:02


    python可以使用redis模块来跟redis交互


    redis模块的使用:

    • 安装模块: pip3 install redis

    • 导入模块:import redis

    • 连接方式:

      • 严格连接模式:r=redis.StrictRedis(host="",port=)

      • 更Python化的连接模式:r=redis.Redis(host="",port=)

      • StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令

      • Redis与StrictRedis的区别是:Redis是StrictRedis的子类,用于向前兼容旧版本的redis-py,并且这个连接方式是更加"python化"的

    • 连接池:

      • 为了节省资源,减少多次连接损耗,连接池的作用相当于总揽多个客户端与服务端的连接,当新客户端需要连接时,只需要到连接池获取一个连接即可,实际上只是一个连接共享给多个客户端。

        import redispool= redis.ConnectionPool(host='localhost',port=6379,decode_responses=True)r=redis.Redis(connection_pool=pool)r2=redis.Redis(connection_pool=pool)r.set('apple','a')print(r.get('apple'))r2.set('banana','b')print(r.get('banana'))print(r.client_list())print(r2.client_list())#可以看出两个连接的id是一致的,说明是一个客户端连接    

    • 操作:

      • redis中get() ==》r.get()

      • redis中mget() ==》r.mget()

      • redis中getset() ==》r.getset()

      • redis中getrange() ==》r.getrange()

      • 其他。。。基本redis的命令名与redis模块中的函数名一致

      • redis中set()  ==>r.set()

      • redis中setnx()  ==>r.set()

      • redis中setex() ==>r.setex()

      • redis中setbit()  ==>r.setbit()

      • redis中mset()  == > r.mset()

      • redis中hset()  ==>r.hset()

      • redis中sadd() == >r.sadd()

      • 其他。。。基本redis的命令名与redis模块中的函数名一致

      • 值的设置和获取,可以参考redis的命令,redis模块中的对应功能的函数名基本与redis中的一致

      • 【注意默认情况下,设置的值或取得的值都为bytes类型,如果想改为str类型,需要在连接时添加上decode_responses=True】

      • 设置值:

      • 获取:

      如果想要了解更多redis命令,可以参考我的另外一篇博文:

      一文学redis操作(记录向)<点击即可跳转>


      import redis
      r=redis.Redis(host='localhost',port=6379,decode_responses=True)
      # r=redis.StrictRedis(host='localhost',port=6379)

      r.set('key','value')
      value=r.get('key')
      # print(type(value))
      print(value)
      r.hset('info','name','lilei')
      r.hset('info','age','18')
      print(r.hgetall('info'))
      r.sadd('course','math','english','chinese')
      print(r.smembers('course'))

      管道:

      一般情况下,执行一条命令后必须等待结果才能输入下一次命令,管道用于在一次请求中执行多个命令。

      • 参数介绍:

        • transaction:指示是否所有的命令应该以原子方式执行。


      import redis,time

      r=redis.Redis(host="localhost",port=6379,decode_responses=True)

      pipe=r.pipeline(transaction=True)

      pipe.set('p1','v2')
      pipe.set('p2','v3')
      pipe.set('p3','v4')
      time.sleep(5)
      pipe.execute()

      事务:

      python中可以使用管道来代替事务:

      • 补充:监视watch:pipe.watch()


      import redis,time
      import redis.exceptions
      r=redis.Redis(host='localhost',port=6379,decode_responses=True)
      pipe=r.pipeline()
      print(r.get('a'))

      try:
       # pipe.watch('a')
       pipe.multi()
       pipe.set('here', 'there')
       pipe.set('here1', 'there1')
       pipe.set('here2', 'there2')
       time.sleep(5)
       pipe.execute()

      except redis.exceptions.WatchError as e:
       print("Error")

      订阅\发布:

        • 发布方:


      import redis
      r=redis.Redis(host="localhost",port=6379,decode_responses=True)

      #发布使用publish(self, channel, message):Publish ``message`` on ``channel``.
      Flag=True
      while Flag:
       msg=input("主播请讲话>>:")
       if len(msg)==0:
         continue
       elif msg=='quit':
         break
       else:
         r.publish('cctv0',msg)
        • 订阅方:

          • 当订阅成功后,第一次接收返回的第一个消息是一个订阅确认消息:python实现与redis交互操作详解


      import redis
      r=redis.Redis(host="localhost",port=6379,decode_responses=True)

      #发布使用publish(self, channel, message):Publish ``message`` on ``channel``.
      Flag=True
      chan=r.pubsub()#返回一个发布/订阅对象
      msg_reciver=chan.subscribe('cctv0')#订阅

      msg=chan.parse_response()#第一次会返回订阅确认信息
      print(msg)
      print("订阅成功,开始接收------")
      while Flag:
       msg=chan.parse_response()#接收消息
       print(">>:",msg[2])#此处的信息格式['消息类型', '频道', '消息'],所以使用[2]来获取

      希望本文所述对大家Python程序设计有所帮助。

      来源:https://www.cnblogs.com/progor/p/8567640.html

      标签:python,redis
      0
      投稿

      猜你喜欢

    • encodeURIComponent用法UrlEncode与URLEncode.encode()

      2009-05-11 12:40:00
    • Python语言中的数据类型-序列

      2023-08-31 14:36:24
    • 使用python打印十行杨辉三角过程详解

      2021-04-16 18:47:24
    • Mysql事物锁等待超时Lock wait timeout exceeded;的解决

      2024-01-22 03:51:08
    • Golang极简入门教程(一):基本概念

      2024-04-26 17:20:46
    • Python爬取豆瓣数据实现过程解析

      2022-01-27 09:12:20
    • “尊重”设计师?

      2009-03-23 18:14:00
    • vue 中Virtual Dom被创建的方法

      2024-05-09 10:41:45
    • Go语言实现AOI区域视野管理流程详解

      2023-06-20 03:21:51
    • Windows10安装Oracle19c数据库详细记录(图文详解)

      2024-01-23 20:13:15
    • python实现从pdf文件中提取文本,并自动翻译的方法

      2021-06-08 18:55:56
    • python 实现学生信息管理系统的示例

      2021-05-01 01:37:24
    • 详解python读取matlab数据(.mat文件)

      2021-03-04 19:29:29
    • ASP UTF-8页面乱码+GB2312转UTF-8 +生成UTF-8格式的文件(编码)

      2011-04-19 11:15:00
    • python字典添加值的方法及实例代码分享

      2023-04-22 09:58:13
    • 互联网产品交互事件分析

      2009-10-06 15:23:00
    • python递归计算N!的方法

      2021-11-11 21:11:31
    • Python实现替换文件中指定内容的方法

      2023-02-23 06:26:03
    • python基础知识之字典(Dict)

      2023-08-25 20:01:44
    • 详解python基础之while循环及if判断

      2023-01-14 20:54:39
    • asp之家 网络编程 m.aspxhome.com