简体   繁体   English

Python进程间通信建议

[英]Python interprocess communication advice

I have 2 python servers running independently of one another but sharing the same database. 我有2个python服务器彼此独立运行但共享相同的数据库。 They need to communicate with each other about when certain changes have been made to the database so the other server (if running) can reload cached data. 他们需要相互通信,了解何时对数据库进行了某些更改,以便其他服务器(如果正在运行)可以重新加载缓存的数据。

What would be my best options for communicating between two such programs? 在两个这样的程序之间进行通信的最佳选择是什么?

I've thought of using sockets but it seems like a lot of work. 我曾经想过使用套接字,但它似乎很多工作。 Either one program will be polling connect whenever the other is off, or they both need to have server/client capabilities. 每当另一个程序关闭时,任何一个程序都将轮询连接,或者它们都需要具有服务器/客户端功能。 I looked into named pipes but didn't see any easy portable solution (needs to run on windows and unix). 我查看了命名管道,但没有看到任何简单的便携式解决方案(需要在Windows和unix上运行)。

You could have each one implement a simple XMLRPC server . 您可以让每个人实现一个简单的XMLRPC服务器 Each one can then execute code in the other, such as telling the other one it needs to update. 然后每个人都可以在另一个中执行代码,例如告诉另一个需要更新的代码。

The easiest way is to use the database itself as a means of communication. 最简单的方法是使用数据库本身作为通信手段。 Add a table for logging updates. 添加用于记录更新的表。 Then, either machine can periodically query to see if the underlying data has been changed. 然后,任一台机器都可以定期查询以查看基础数据是否已更改。

Another easy form of communication is email using the smtplib module . 另一种简单的通信形式是使用smtplib模块的电子邮件。 Our buildbots and version control repository use this form of communication. 我们的buildbots和版本控制存储库使用这种形式的通信。

If you want something with a little more "industrial" strength, consider using RabbitMQ or somesuch for messaging between servers. 如果你想要一些具有更多“工业”优势的东西,可以考虑使用RabbitMQ或其他服务器之间的消息传递。

I agree that sockets are usually too low-level. 我同意套接字通常太低级。 If you investigate "RabbitMQ", you should also investigate celery . 如果您调查“RabbitMQ”,您还应该调查芹菜 It can use RabbitMQ as a back-end, but it can also use the database, and it neatly encapsulates the messaging mechanism. 它可以使用RabbitMQ作为后端,但它也可以使用数据库,它整齐地封装了消息传递机制。 It is also integrated with django and gevent. 它还与django和gevent集成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM