本文簡要介紹ruby語言中 PTY模塊
的用法。
創建和管理偽終端 (PTY)。另見en.wikipedia.org/wiki/Pseudo_terminal
PTY
允許您使用 ::open
或 ::spawn
分配具有特定命令的新終端。
示例
在此示例中,我們將更改 factor
命令中的緩衝類型,假設 factor 使用 stdio 進行 stdout 緩衝。
如果使用 IO.pipe
而不是 PTY.open
,則此代碼將死鎖,因為因子的標準輸出已完全緩衝。
# start by requiring the standard library PTY
require 'pty'
master, slave = PTY.open
read, write = IO.pipe
pid = spawn("factor", :in=>read, :out=>slave)
read.close # we dont need the read
slave.close # or the slave
# pipe "42" to the factor command
write.puts "42"
# output the response from factor
p master.gets #=> "42: 2 3 7\n"
# pipe "144" to factor and print out the response
write.puts "144"
p master.gets #=> "144: 2 2 2 2 3 3\n"
write.close # close the pipe
# The result of read operation when pty slave is closed is platform
# dependent.
ret = begin
master.gets # FreeBSD returns nil.
rescue Errno::EIO # GNU/Linux raises EIO.
nil
end
p ret #=> nil
相關用法
- Ruby PTY.open用法及代碼示例
- Ruby PrettyPrint.current_group用法及代碼示例
- Ruby Pathname.<=>用法及代碼示例
- Ruby Pathname.children用法及代碼示例
- Ruby Process.groups用法及代碼示例
- Ruby Process.wait2用法及代碼示例
- Ruby Process.getpgrp用法及代碼示例
- Ruby Proc.eql?用法及代碼示例
- Ruby PrettyPrint.genspace用法及代碼示例
- Ruby PKey.sign_raw用法及代碼示例
- Ruby Profiler模塊用法及代碼示例
- Ruby Process.setproctitle用法及代碼示例
- Ruby PPMethods.comma_breakable用法及代碼示例
- Ruby Process.setrlimit用法及代碼示例
- Ruby Proc.prc ==用法及代碼示例
- Ruby Profiler.raw_data用法及代碼示例
- Ruby Process.uid用法及代碼示例
- Ruby Pathname.descend用法及代碼示例
- Ruby Process.pid用法及代碼示例
- Ruby Proc.ruby2_keywords用法及代碼示例
- Ruby Pathname.getwd用法及代碼示例
- Ruby PKey.generate_parameters用法及代碼示例
- Ruby Proc.new用法及代碼示例
- Ruby Process.detach用法及代碼示例
- Ruby Pathname.ascend用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 PTY模塊。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。