I would like to know the best way to send data from OCaml to Python and get response from Python back to OCaml.
One naive method I can think is as follows.
1) In OCaml, write data into a file (input.txt) on file system.
2) In OCaml, run python, which opens input.txt and reads the data and write the execution result into output.txt
3) In OCaml, open output.txt and read the result
Is there other easy ways to do this kind of task?
Thanks in advance.
This is a general question on how to talk between two programs, written in different languages. Actually, the problem should be further subdivided into two separate subproblems:
The first question is very general. You can use sockets of different kinds, pipes, shared memory, zmq library, or whatever. It is hard to give any advice here, since all choices has their cons and pros. Maybe using http
over TCP
socket is not a bad choice, since they're ubiquitous, and there excellent libraries on both sides of the pipe. Another approach is to use pipes, and to call one or another part using popen
system call, that will create a process for you and return pipes that you can read or write from. As an extension of the previous, you can even use python
as a library, and call the interpreter directly from your ocaml program, passing strings with code. Depending on your task it can be suitable or not.
The second question, is about how to serialize OCaml native type, like int
to bytes, and then read this bytes as python native int
type, and vice verse. There're some solutions here, not so many though. You can use json
with ezjsonm
library on the OCaml (on python there is a json
), you can use cap'n'proto
it has bindings in OCaml and Python. Also, there are well known Google Protocol buffers, aka protobufs, with several bindings in OCaml, including piqi
library, that can also serialize into many formats.
There is another approach, you can make you OCaml program to be a dynamic library exposing interface in C
, then create a python module, and call it directly as library. This is not very easy, because it requires to mess with build system, so I do not recommend it until you have performance requirements.
The simplest way is indeed to use a file. You can avoid writing to disk by using a named pipe; on a *nix system, put the file in /tmp/
anyway.
A still relatively simple alternative is to use a network socket.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With