This is a general question about how to talk between two programs written in different languages. In fact, the problem should be divided into two separate subtasks:
- Which transport to use? (file, socket, etc.)
- How to serialize and deserialize data.
The first question is very general. You can use different types of sockets, channels, shared memory, zmq library or whatever. It is difficult to give any advice here, since all options have their disadvantages and advantages. Perhaps using the http over TCP socket is not a bad choice as they are ubiquitous and there are great libraries on either side of the pipe. Another approach is to use channels and call one or another part using the popen system call, which will create a process for you and return channels that you can read or write. As an extension of the previous one, you can even use python as a library and call the interpreter directly from your ocaml program, passing lines with code. Depending on your task, it may or may not be suitable.
The second question is how to serialize a native OCaml type, such as int , into bytes, and then read these bytes as a native python-based int type and vice versa. There are some solutions here, but not many. You can use json with the ezjsonm library in OCaml (python has json ), you can use cap'n'proto , it has bindings in OCaml and Python. In addition, there are well-known buffers of the Google protocol, for example protobuffs, with several links in OCaml, including the piqi library, which can also be serialized in many formats.
There is another approach, you can make the OCaml program a dynamic library displaying the interface in C , then create a python module and call it directly as a library. It is not very simple because it requires a mess with the assembly, so I do not recommend it until you have performance requirements.
source share