Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between OCaml and Python

Tags:

python

ocaml

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.

like image 796
Mark Avatar asked Jan 15 '15 14:01

Mark


2 Answers

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:

  1. What transport to use? (file, socket, etc)
  2. How to serialize and deserialize data.

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.

like image 82
ivg Avatar answered Sep 28 '22 10:09

ivg


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.

like image 24
qsantos Avatar answered Sep 28 '22 09:09

qsantos