Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicating with Julia through ruby PTY

Tags:

ruby

pty

julia

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")
like image 710
user3696058 Avatar asked May 09 '26 04:05

user3696058


1 Answers

Ok I think I figured out what is happening:

  1. you need to give julia time to start up.

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

like image 194
John C Avatar answered May 10 '26 20:05

John C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!