使用 Redis、Python 和 PHP 进行基于事件的邮件发送

Event-based mailing with Redis, Python and PHP

我想经常从 Python 组件向 Redis 服务器发布消息。然后,在另一个也可以访问此 Redis 服务器的 PHP 组件中,我想根据存储在 Redis 中的消息向用户发送电子邮件。如果我没记错的话,有两种方法可以做到这一点:推拉和推推设计:

拉设计

PHP组件频繁向Redis服务器发起请求,当有新消息时,进行动作。

推送设计:

我们不需要从 PHP 组件发出这些频繁的请求:每当在 Redis 中发布新消息时,都可以触发来自 PHP 组件的操作。

我的问题

我们如何实现Push-Push设计?当我阅读有关 Pub-Sub 模型的 Redis 文档时,我没有找到任何相关信息。所以我真的不明白我们如何以另一种方式触发一个动作,然后向 redis 服务器发出频繁的请求。

您需要通过使用 nohupsupervisorupstart 等解决方案 运行 "long-running" php 进程作为守护进程。这个进程将作为守护进程继续工作以消耗你的 redis channel。 Python 将继续生产,php 将继续消费。

有几个库 运行 php 作为守护进程处理,例如 reactphp or if you are using a framework such as laravel, it offers a good pub/sub interface

会是这样的;

Php 部分将 subscribemychannel

127.0.0.1:6379> SUBSCRIBE mychannel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "mychannel"
3) (integer) 1

python 将 publish 变为 mychannel

127.0.0.1:6379> publish mychannel myemailjson
(integer) 1
127.0.0.1:6379> publish mychannel myanotheremailjson
(integer) 1
127.0.0.1:6379>

与此同时,您的 php 进程将收到该消息

1) "message"
2) "mychannel"
3) "myemailjson"
1) "message"
2) "mychannel"
3) "myanotheremailjson"

在那个 subscriber php 过程中,您将 call/trigger/dispatch 您的电子邮件发送作业(可能是异步的)每当它收到一条消息时。