[Redis-Python]发布订阅通过Redis异步发送邮件

接收订阅

#!/usr/bin/env pyhton
# coding:utf-8
# @Time     : 2020-02-16 21:36
# @Author   : LeoShi
# @Site     : 
# @File     : redis_demo.py
# @Software : MacOS Python 3.7

import redis

# 创建链接
# 返回 b'peigy2020'
# redis_connect = redis.Redis(host='172.16.1.111', port=6379, password='password')
# decode_responses=True 返回字符串
redis_connect = redis.Redis(host='172.16.1.111', port=6379, password='password', decode_responses=True)

# 接收订阅
catch = redis_connect.pubsub()
# 接收频道
catch.subscribe('email')
while 1:
    for item in catch.listen():
        if item['type'] == 'message':
            data = item['data']
            print(data)
            # 发送邮件

发布订阅

#!/usr/bin/env pyhton
# coding:utf-8
# @Time     : 2020-02-16 22:19
# @Author   : LeoShi
# @Site     : 
# @File     : redis_pulish.py
# @Software : MacOS Python 3.7

import redis

# 建立连接
redis_connect = redis.Redis(host='172.16.1.111', port=6379, password='password', decode_responses=True)

# 发布订阅
for i in range(3):
    redis_connect.publish('email','This is message{}'.format(i))

接收信息结果

This is message0
This is message1
This is message2
posted @ 2020-02-16 22:47  LeoShi2020  阅读(381)  评论(0编辑  收藏  举报