首页 > 编程语言 > 详细

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

时间:2020-02-17 00:41:07      阅读:128      评论:0      收藏:0      [点我收藏+]

接收订阅

#!/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

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

原文:https://www.cnblogs.com/leoshi/p/12319261.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!