【问题标题】:Communicating with Julia through ruby PTY通过 ruby​​ PTY 与 Julia 交流
【发布时间】:2014-06-01 03:51:40
【问题描述】:

我正在尝试通过标准输入将命令发送到 julia 会话。谁能给我一些关于为什么这段代码似乎从来没有在朱莉娅这边执行任何事情的指示?似乎命令已传递给 julia,但从未真正运行,或者 julia 从未将其输出传递到输出流......我希望最终在输出流中看到 4(2 + 2 的结果)。 . 有什么想法吗?

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")

【问题讨论】:

标签: ruby julia pty


【解决方案1】:

好的,我想我知道发生了什么:

  1. 您需要给 julia 时间启动。

  2. 您需要发送一个带有 \n 的 \r 来告诉 julia 阅读该行。

这对我有用:

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")

注意事项:

我认为使用@output.expect("julia\>") 会更好,而不是笨重的 sleep 5。但睡眠证明了它失败的原因。

【讨论】:

  • 是的,明白了...我弄乱了\r\n,但没有弄乱睡眠...我想我以为这就像我在普通终端上看到的一样, 并在准备好时响应输入(例如我如何在 vim 加载之前输入命令)。谢谢大佬!
猜你喜欢
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
相关资源
最近更新 更多