I am trying to basically send commands through stdin to a julia session. Can anyone give me some pointers on why this bit of code never seems to be executing anything on julia's side? It seems like the command gets passed to julia, but never actually runs, or julia never delivers it's output to the output stream... I would expect to eventually see 4 (the result of 2 + 2) in the output stream here... Any thoughts?
require 'pty'
require 'expect'
class Session
def initialize
@output, @input, @pid = PTY.spawn('julia -q')
end
def exec(cmd)
@input.write(cmd + "\n")
@output.each { |line| print line }
end
end
session = Session.new()
session.exec("2 + 2")
Ok I think I figured out what is happening:
you need to give julia time to start up.
you need to send a \r with the \n to tell julia to read the line.
This works for me:
require 'pty'
require 'expect'
class Session
def initialize
@output, @input, @pid = PTY.spawn('julia -q')
sleep 5
# @output.expect(/julia\>/) would be nicer!
end
def exec(cmd)
@input.write(cmd + "\r\n" # This is control-m
@output.each { |line| print line }
end
end
session = Session.new()
session.exec("2 + 2")
Notes:
Instead of the clunky sleep 5 I think it would be better with @output.expect("julia\>"). But the sleep proves why it fails.
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