Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way for Clojure and Python programs to share information?

I have two questions, one a subset of the other.

1) What is the best way to pass information between a Python and Clojure program. That question could also be extended to any jvm language like Scala. These programs would be running under Linux.

2) What would be the best way to do this under Windows?

The details are as follows. I would like a Python program to launch a Clojure program, know when the Clojure program has completed, and retrieve the results of running the Clojure program.

I know that between a Python and Clojure program, information could be shared in a database table created specifically for that purpose of depositing information when the Clojure program has completed, or even doing something as clunky as leaving a results file in a known directory.

Given this is Linux, I can probably share information using a pipe, but I am specifically wondering if there is a module supported by both Python and Clojure that would facilitate inter-program communication.

Thank you.

like image 902
octopusgrabbus Avatar asked Jul 25 '12 14:07

octopusgrabbus


3 Answers

I'm not sure what your Clojure program is doing, but if you are simply looking to execute Clojure code from Python, then perhaps then Clojure-Py may help:

https://github.com/halgari/clojure-py

As an example, you can do this in Python:

>>> import clojure.core

>>> clojure.core.cons(1, None)
(1)

Disclaimer, I'm the author of clojure-py

like image 64
Timothy Baldridge Avatar answered Nov 18 '22 11:11

Timothy Baldridge


Another IPC approach would be to use sockets. I created a very simple socket server on python that accepts strings and calls a function on it. Then, the clojure instance can connect to the python server and send clojure forms over as data. Using pyclj, use the pyclj reader to turn the clojure data into python forms, then process it, and then return back clojure data using the pyclj writer. This approach is more clojure friendly as you are just working with clojure data all the time. This also allows more flexibility in where the python and clojure instances are running, and provides a better interface for asynchronous communication.

https://github.com/sunng87/pyclj

like image 2
bmillare Avatar answered Nov 18 '22 11:11

bmillare


If performance is important then Protocol Buffers is a good option. Using protobuf from Clojure is covered well in Protocol Buffers with Clojure and Leiningen.

like image 1
Dimagog Avatar answered Nov 18 '22 10:11

Dimagog