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.
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
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
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.
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