Communication between OCaml and Python

I would like to know the best way to send data from OCaml to Python and get a response from Python to OCaml.

One naive method that I can think of is as follows.

1) In OCaml, write data to a file (input.txt) in the file system.

2) In OCaml, run python, which opens the input.txt file and reads the data and writes the execution result to the output.txt file

3) In OCaml open the file output.txt and read the result

Are there any other simple ways to accomplish this task?

Thanks in advance.

+5
source share
3 answers

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.

+8
source

The easiest way is to use a file. You can avoid writing to disk using named pipe ; on a * nix system, put the file in /tmp/ anyway.

A relatively simple alternative so far is to use a network socket.

+2
source

You can use PyCaml . It allows you to pack OCaml code as a python extension (called from Python) and vice versa, so you can call Python code from OCAML.

+1
source

Source: https://habr.com/ru/post/1211165/


All Articles