zoukankan      html  css  js  c++  java
  • redis(2)事务的订阅与发布

    一、shell终端进行事务的订阅与发布(异步

    发布 : publish channel message
    [root@localhost ~]# redis-cli -p 6379 -h 192.168.42.7
    192.168.42.7:6379> publish test1 woaini
    (integer) 1
    
    
    订阅 : subscribe channel
    192.168.42.7:6379> select 9    切换到9数据库
    OK
    192.168.42.7:6379[9]> subscribe test1
    Reading messages... (press Ctrl-C to quit)
    1) "subscribe"
    2) "test1"
    3) (integer) 1
    1) "message"
    2) "test1"
    3) "woaini"

    二、python第三方执行事物的订阅与发布

    pip install redis

    # 从redis包中导入redis类
    from redis import Redis
    # 初始化redis实例变量
    xtredis = Redis(host='192.168.42.7',port=6379)
    【字符串】
    # 添加一个值进去,并且设置过期时间为60秒,如果不设置,则永远不会过期
    # xtredis.set('username','xiaotuo',ex=60)
    # 获取一个值
    # xtredis.get('username')
    # 删除一个值
    # xtredis.delete('username')
    【列表】
    xtredis.lpush('names','haha','xixi','dudu','momo')
    xtredis.rpush('names','jam','hsiao','jamhsiao')
    xtredis.rpop('names')
    xtredis.lpop('names')
    xtredis.lindex('names',0)
    关于集合与哈希表的使用就不再概述了,基本与上述方法类似,有兴趣的话自己去试。接下来说一下事务的订阅与发布,订阅与发布要在不同的py文件中
    #发布
    from redis import Redis xtredis = Redis(host='192.168.42.7', port=6379) while True: xtredis.publish('email', input("===>:")) # 订阅 ps = xtredis.pubsub() ps.subscribe('email') while True: for item in ps.listen(): ps.listen()是一个生成器 if item['type'] == 'message': data = item.get('data') print(data.decode('utf-8')) 解码
  • 相关阅读:
    Linux定时任务调度
    Linux组管理和权限管理
    Linux压缩和解压缩类指令
    leetcode:Compare Version Numbers
    leetcode:Valid Palindrome
    Majority Element
    Min Stack
    leetcode:Intersection of Two Linked Lists(两个链表的交叉点)
    leetcode:Factorial Trailing Zeroes
    leetcode:Rotate Array
  • 原文地址:https://www.cnblogs.com/daisyyang/p/10874405.html
Copyright © 2011-2022 走看看