C++ 之间的简单 IPC和Python(跨平台)
我有一个在后台运行的 C++ 进程,它将很少生成“事件”,而在同一机器上运行的 Python 进程则需要拾取这些事件。
- C端的代码需要尽可能的轻量级。
- Python 端是只读的。
- 实施必须是跨平台的。
- 发送的数据非常简单。
我有什么选择?
谢谢
I have a C++ process running in the background that will be generating 'events' infrequently that a Python process running on the same box will need to pick up.
- The code on the C side needs to be as lightweight as possible.
- The Python side is read-only.
- The implementation must be cross-platform.
- The data being sent is very simple.
What are my options?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
zeromq——仅此而已。将消息编码为字符串。
但是,如果您想从库中获取序列化,请使用 protobuf 它将生成 Python 和C++。您可以在两端使用 SerializeToString() 和 ParseFromString() 函数,然后通过 ZeroMq 传输字符串。
问题解决了,因为我怀疑任何其他解决方案都会更快,而且任何其他解决方案都不会那么容易连接和理解。
如果想为 rpc 使用特定的系统原语,例如 Windows 上的命名管道和 unix 上的 Unix 域套接字,那么您应该查看 Boost::ASIO。但是,除非您具有 (a) 网络背景,并且 (b) 对 C++ 有很好的理解,否则这将非常耗时
zeromq -- and nothing else. encode the messages as strings.
However, If you want to get serialiazation from a library use protobuf it will generate classes for Python and C++. You use the SerializeToString() and ParseFromString() functions on either end, and then pipe the strings via ZeroMq.
Problem solved, as I doubt any other solution is faster, and neither will any other solution be as easy to wire-up and simple to understand.
If want to use specific system primitives for rpc such as named pipes on Windows and Unix Domain Sockets on unix then you should look at Boost::ASIO. However, unless you have (a) a networking background, and (b) a very good understanding of C++, this will be very time consuming
使用 zeromq,它非常简单。
Use zeromq, it's about as simple as you can get.
Google 的 protobuf 是一个很棒的程序间 RPC 库。它生成 Python 和 C++。
如果您需要分布式消息传递系统,您还可以使用 RabbitMQ、zeromq,或 ActiveMQ。有关消息队列库的讨论,请参阅此问题。
Google's protobuf is a great library for RPC between programs. It generates bindings for Python and C++.
If you need a distributed messaging system, you could also use something like RabbitMQ, zeromq, or ActiveMQ. See this question for a discussion on the message queue libraries.
另一种选择是使用
ctypes
从 Python 代码中调用 C 代码 模块而不是单独运行两个程序。Another option is to just call your C code from your Python code using the
ctypes
module rather than running the two programs separately.您的数据有多复杂?如果很简单,我会将其序列化为字符串。如果它相当复杂,我会使用 JSON。 TCP是一种很好的跨平台IPC传输。既然你说这个IPC很少见,那么性能也不是很重要,TCP+JSON就可以了。
How complex is your data? If it is simple I would serialize it as a string. If it was moderately complex I would use JSON. TCP is a good cross-platform IPC transport. Since you say that this IPC is rare the performance isn't very important, and TCP+JSON will be fine.
您可以使用 Google GRPC 来实现此目的
You can use Google GRPC for this
我会说你创建一个 DLL 来管理两者之间的通信。 python将加载DLL并调用getData()等方法,DLL将依次与进程通信并获取数据。
那应该不难。
您还可以使用 XML 文件或 SQLite 数据库或任何数据库来查询数据。守护进程将更新数据库,Python 将继续查询。可能有一个字段用于指示DB中的数据是否已经被守护进程更新,然后Python将查询。
当然这取决于性能和精度因素!
I will say you create a DLL that will manage the communication between the two. The python will load DLL and call method like getData() and the DLL will in turn communicate with process and get the data.
That should not be hard.
Also you can use XML file or SQLite database or any database to query data. The daemon will update DB and Python will keep querying. There might be a filed for indicating if the data in DB is already updated by daemon and then Python will query.
Of course it depends on performance and accuracy factors!