簡體   English   中英

有沒有類似Python的pty.fork for Ruby?

[英]Is there anything like Python's pty.fork for Ruby?

我正在嘗試將以下Python代碼移植到Ruby:

import pty

pid, fd = pty.fork
if pid == 0:
  # figure out what to launch
  cmd = get_command_based_on_user_input()

  # now replace the forked process with the command
  os.exec(cmd)
else:
  # read and write to fd like a terminal

因為我需要像終端一樣讀寫子進程,所以我理解我應該使用Ruby的PTY模塊來代替Kernel.fork。 但它似乎沒有等效的fork方法; 我必須將命令作為字符串傳遞。 這是我最接近Python的功能:

require 'pty'

# The Ruby executable, ready to execute some codes
RUBY = %Q|/proc/#{Process.id}/exe -e "%s"|

# A small Ruby program which will eventually replace itself with another program. Very meta.
cmd = "cmd=get_command_based_on_user_input(); exec(cmd)"

r, w, pid = PTY.spawn(RUBY % cmd)
# Read and write from r and w

顯然,其中一些是特定於Linux的,這很好。 顯然有些是偽代碼,但這是我能找到的唯一方法,而且我只有80%肯定它會起作用。 Ruby當然有更清潔的東西嗎?

重要的是“get_command_based_on_user_input()”不會阻止父進程,這就是為什么我把它粘在子進程中。

你可能尋找http://ruby-doc.org/stdlib-1.9.2/libdoc/pty/rdoc/PTY.htmlhttp://www.ruby-doc.org/core-1.9.3/ Process.html#method-c-fork在Ruby中使用雙叉創建一個守護進程

我打開一個帶有主進程的PTY,fork並用STDIN.reopen重新連接到PTY的PTY。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM