Call a program written in python in a program written in Ocaml

I wanted to ask if you can call a program written in Ocaml, a program written in python, and if so, how can I do this?

+3
source share
6 answers

What exactly do you intend to do? Do you want to run it and forget about it? Then do fork / exec. Do you want to run it and wait for it to end, but otherwise do nothing? Then use Sys.command. Do you want to read / write? Then used Unix.open_process*(or Unix.create_process*).

For example, if I want to run lsand print the results, I can do this:

let ls = Unix.open_process_in "ls"
try
  while true do
    Printf.printf "%s\n" (input_line ls)
  done
with End_of_file -> ()
Unix.close_process_in ls
+5
source

, ( Python ):

Pycaml: Python OCaml ( C) Python OCaml.

+2

Sys.command, Sys.command "python foo.py", , python , foo.py .

+2

( ;)), Unix-, C, :

let program = "path_to_python_program_exe" in

match Unix.fork () with
    | 0 -> (try
          Unix.execvp program [|program; "any_more_args_here"|]
       with
          _ -> printf "%s" "error while execv\n"; exit (-1))
    | -1 -> printf "%s" "error accured on fork\n"
    | _ -> ignore (wait ()); printf "%s" "parent exit...\n"

, unix.cma : ocamlc unix.cma you_ml.ml

+2

, .

+1

, pythons os.system() , . .

-1

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


All Articles