5

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.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131

5 Answers5

5

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

Timothy Baldridge
  • 10,455
  • 1
  • 44
  • 80
2

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

bmillare
  • 4,183
  • 2
  • 23
  • 42
1

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.

Dimagog
  • 1,785
  • 19
  • 14
0

I'm not familiar with Clojure, but you could probably pipe json to it using the python subprocess module.

import json
from subprocess import Popen, PIPE

json_data = json.dumps({"key":"value"})
p = subprocess.Popen("closure-execution", stdout=PIPE, stdin=PIPE, sterr=PIPE)
# stuff some data into the pipe and wait for the process to end:
stdout, sterr = p.communicate(json_data)

or somthing like that...

monkut
  • 42,176
  • 24
  • 124
  • 155
0

One option would be to use Jython to run your Python code in the same JVM as Clojure - then the interop would be substantially easier.

The interop should also be OS-independent in this case, which is a nice bonus.

See:

Community
  • 1
  • 1
mikera
  • 105,238
  • 25
  • 256
  • 415