<p>如果您需要在Python和OCaml代码之间进行强大的通信,那么确实应该有两个独立的“主”进程(将它们视为网络节点)。在</p>
<p>正如@svenmarnach已经提到的,实现这一点的一个好选择是通过基于JSON的协议将这两个进程链接起来。在</p>
<p>更方便的方法是使用Google的gRPC框架(<a href="https://grpc.io/" rel="nofollow noreferrer">https://grpc.io/</a>)并通过Protobuf消息(<a href="https://developers.google.com/protocol-buffers/" rel="nofollow noreferrer">https://developers.google.com/protocol-buffers/</a>)进行通信。这个框架非常简单。不幸的是,目前还没有对OCaml的支持,但我认为您可以将OCaml <code>main</code>精简为Python层,或者将其转换为JS。然后,您只需将您的函数连接到gRPC接口。在</p>
<p>系统的外观如下:</p>
<pre><code>+ + + + + -Thin Python wrapper / JS wrapper -+
| Your | | | | + + |
| Python |<->| gRPC |<->| | Your OCaml app | |
| app | | | | + + |
+ + + + + +
</code></pre>
<p><strong>p.S.</strong>我在一个类似于您的问题中使用相同的方法(但是GUI是用Java编写的)。我认为它非常方便,开发速度快,易于扩展。在</p>
<p><strong>p.p.S.</strong>在这方面你并不孤单:)。这是一篇有趣的节选自a(前?)的论文谷歌员工(<a href="https://arxiv.org/abs/1702.01715" rel="nofollow noreferrer">https://arxiv.org/abs/1702.01715</a>):</p>
<blockquote>
<p>Software engineers at Google are strongly encouraged to program in one of five
officially-approved programming languages at Google: C++, Java, Python, Go, or JavaScript.</p>
<p>Interoperation between these different programming languages is done mainly using Protocol
Buffers. Protocol Buffers is a way of encoding structured data in an efficient yet extensible
way. It includes a domain-specific language for specifying structured data, together with a
compiler that takes in such descriptions and generates code in C++, Java, Python, for
constructing, accessing, serializing, and deserializing these objects. Google’s version of
Protocol Buffers is integrated with Google’s RPC libraries, enabling simple cross-language
RPCs, with serialization and deserialization of requests and responses handled automatically by
the RPC framework.</p>
</blockquote>