1

My requirement is to form an integration between scala and python. How do I start "python.exe" and executes python commands from scala.

I have tried the code snippet from following link, but it doesn't seems to be working. Could you please help me this ?

How does the “scala.sys.process” from Scala 2.9 work?

Thanks in Advance !

Community
  • 1
  • 1
yoganathan k
  • 213
  • 4
  • 16
  • Do you *have* to use CPython? Why not just use Jython and be done with it? – Jörg W Mittag Jul 21 '15 at 12:56
  • Thanks for your reply @Jorg.... Since I faced some issues with 'jep' library, I moved to this process. Is there any other option available ? – yoganathan k Jul 21 '15 at 12:59
  • 2
    @yoganathank : "it doesn't seem to be working" **how**? Please provide the exact code you're using as well as the erroneous output. – mikołak Jul 21 '15 at 13:06

2 Answers2

1

The process API is quite contrived and absolutely horribly (= not) documented. I often go back to the Java API because it makes more sense to me.

Here is an example. Overwriting the os variable to "store" the output-stream seems like an idiotic approach (in Java you can just query the output-stream from the process). Perhaps the designers of the API can enlighten us what they were thinking. Probably there is a more elegant solution:

import sys.process._

var os: java.io.OutputStream = _
val python = Process(Seq("python","-i")).run(BasicIO.standard(os = _) /* WTF? */)

def pushLine(s: String): Unit = {
  os.write(s"$s\n".getBytes("UTF-8"))
  os.flush()
}

pushLine("1+1")
pushLine("exit()")
0__
  • 66,707
  • 21
  • 171
  • 266
1

I recently had to undertake a very similar project. Rather than just executing a python script and parsing the output, i needed to start up some python code and send commands to it to execute and respond back. After some time trying to do this communication over STDIN/STDOUT, I opted for using ZeroMQ as my interop communications channel

By doing this, the execution of code became as simple as

val process = s"python commandRunner.py $port".run()

where $port is the port on which my code is listening for the python code to connect back to me via ZeroMQ, while the commandRunner script runs a simple ZeroMQ event-loop

user3666197
  • 1
  • 6
  • 50
  • 92
Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
  • Yes, ZeroMQ is a very powerfull ( ... and fast to get used to ) framework for trully distributed computing [+1 Arne. – user3666197 Jul 22 '15 at 06:08