Есть ли способ IO трубы? - PullRequest
1 голос
/ 18 июня 2019

Я пытаюсь передать 2 объекта ввода-вывода, я пришел из nodejs, и мы можем сделать что-то вроде этого:

const child_process = require('child_process')

const shell = child_process.spawn('/bin/sh')

shell.stdout.pipe(process.stdout)
shell.stdin.write('pwd\n')
shell.stdin.write('ls\n')
/* write all command i want */

, и я ищу сделать то же самое в кристалле

я знаю, что для текущего примера мы можем написать

shell = Process.new("/bin/sh", input: Process::Redirect::Pipe, output: STDOUT, error: STDOUT)

shell.input << "ls\n"
shell.input << "pwd\n"
# all commands i want

, но по какой-то причине передача TCPSocket в Process.new ввод / вывод / ошибка не очень хорошо работает (см. здесь, если у вас есть время Процесс иTCPSocket не закрывается должным образом в кристалле )

, поэтому я ищу альтернативный способ, который будет выглядеть так:

shell = Process.new("/bin/sh", input: Process::Redirect::Pipe, output: Process::Redirect::Pipe, Process::Redirect::Pipe)

shell.output.pipe(STDOUT) # not the crystal pipe but like the nodejs pipe

shell.input << "ls\n"
shell.input << "pwd\n"
# all commands i want

1 Ответ

2 голосов
/ 01 июля 2019

Вы можете использовать IO.copy внутри сопрограммы:

shell = Process.new("/bin/sh", input: :pipe, output: :pipe, error: :pipe)

spawn { IO.copy shell.output, STDOUT }
spawn { IO.copy shell.error, STDERR }

shell.input << "ls /\n"
shell.input << "pwd\n"

shell.wait

https://carc.in/#/r/75z4

...