<p>If you need a strong communication between your Python and OCaml code, there should indeed be two separate "master" processes (think of them as network nodes).</p>

<p>As @Sven Marnach already mentioned, a good option to implement this is to link these two processes via JSON-based protocol.</p>

<p>A more convenient approach would be to use Google's gRPC framework (<a href="https://grpc.io/" rel="nofollow noreferrer">https://grpc.io/</a>) and communicate via Protobuf messages (<a href="https://developers.google.com/protocol-buffers/" rel="nofollow noreferrer">https://developers.google.com/protocol-buffers/</a>). The framework is very handly. Unfortunately, there is no support for OCaml yet, but I think you can thin wrap your OCaml <code>main</code> into thin Python layer, or translate it to JS. Then, all you need to do is just to connect your functions to gRPC interfaces.</p>

<p>Here is how the system would look like:</p>

<pre><code>+----------+   +------+   +---Thin Python wrapper / JS wrapper---+
|   Your   |   |      |   |  +--------------------------------+  |
|  Python  |&lt;-&gt;| gRPC |&lt;-&gt;|  |        Your OCaml app          |  |
|   app    |   |      |   |  +--------------------------------+  |
+----------+   +------+   +--------------------------------------+
</code></pre>

<p><strong>P.S.</strong> I'm using the same approach in a problem similar to yours  (but GUI is in Java). I'd say that it is very convenient, fast to develop, and easily extendable.</p>

<p><strong>P.P.S.</strong> You are not alone in this :). Here is an interesting excerption from a paper by a (former?) Google employee (<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>
Integrating OCaml in python - How to hold an ocaml session from python?
<p>I would like a python GUI to have an OCaml process in the background. I would like to keep a single session throughout the program's lifetime, and depending on user inputs, call some OCaml commands and retrieve OCaml's output. Some OCaml variables and structures may be defined along the way so I would like to maintain a single ongoing session.</p>

<p>My solution was to hold an OCaml toplevel process using popen and interact with its stdin and stdout. This works purely for me for several reasons:
1. I don't know when is the OCaml calculation done and can't tell if it's output is complete or there is more to come (especially so if the evaluation takes some time, and if multiple OCaml commands were invoked).
2. I have no inherent way of telling whether the OCaml command ran smoothly or maybe there were OCaml warnings or errors.
3. I lose the structure of OCaml's output. For example, if the output spreads over several lines, I can't tell which lines were broken due to line size, and which were originally separate lines. </p>

<p>I know there are some discussions and some packages for combining python with OCaml, but they all run python commands from OCaml, and I need the opposite. </p>