我想从我的程序中生成(fork?)多个Python脚本(也用Python编写).
我的问题是我想为每个脚本专用一个终端,因为我将使用它来收集它们的输出pexpect.
我已经尝试使用pexpect,os.execlp以及os.forkpty但是他们都没有做,因为我期望的那样.
我想生成子进程并忘记它们(它们将处理一些数据,将输出写入我可以读取的终端,pexpect然后退出).
有没有图书馆/最佳实践/等.完成这份工作?
ps在你问我为什么要写STDOUT并从中读取之前,我会说我不写STDOUT,我读了输出tshark.
我计划用 Python 创建一个长时间运行的后台进程,但我仍然不确定是否使用os.spawnle或线程。我只读过它,因此我对生成或线程没有太多经验。何时使用哪个有什么经验法则吗?
非常感谢
我正在研究简单的 CLI,它的功能之一是将 ssh 运行到远程服务器。这是我的意思的一个例子:
#!/usr/bin/env node
var spawn = require('child_process').spawn;
var readline = require('readline');
var iface =readline.createInterface({
input: process.stdin,
output: process.stdout
});
iface.setPrompt("host> ");
iface.on("line", function(line) {
if (line.trim() == "") {
return iface.prompt();
}
var host = line.split()[0];
iface.pause(); // PAUSING STDIN!
var proc = spawn('ssh', [ host, '-l', 'root', '-o', 'ConnectTimeout=4' ], { stdio: [0,1,2] });
proc.on("exit", function(code, signal) {
iface.prompt();
});
});
iface.prompt();
Run Code Online (Sandbox Code Playgroud)
I use iface.pause() to make child process able to read from STDIN exclusively, if …
我想在以下方法上测试返回值和IO输出:
defmodule Speaker do
def speak do
receive do
{ :say, msg } ->
IO.puts(msg)
speak
_other ->
speak # throw away the message
end
end
end
Run Code Online (Sandbox Code Playgroud)
在ExUnit.CaptureIO文档中,有一个示例测试,它执行此操作,如下所示:
test "checking the return value and the IO output" do
fun = fn ->
assert Enum.each(["some", "example"], &(IO.puts &1)) == :ok
end
assert capture_io(fun) == "some\nexample\n"
end
Run Code Online (Sandbox Code Playgroud)
鉴于此,我认为我可以编写以下测试,执行类似的操作但使用spawned过程:
test ".speak with capture io" do
pid = Kernel.spawn(Speaker, :speak, [])
fun = fn ->
assert send(pid, { :say, …Run Code Online (Sandbox Code Playgroud) 我正在将文档转换为内存中的pdf(unoconv)并在终端中打印(pdftotext):
unoconv -f pdf --stdout sample.doc | pdftotext -layout -enc UTF-8 - out.txt
Run Code Online (Sandbox Code Playgroud)
工作中.现在我想用这个命令child_process.spawn:
let filePath = "...",
process = child_process.spawn("unoconv", [
"-f",
"pdf",
"--stdout",
filePath,
"|",
"pdftotext",
"-layout",
"-enc",
"UTF-8",
"-",
"-"
]);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,只有第一个命令(在|之前)正在工作.我有可能做我正在尝试的事情吗?
谢谢.
最新情况:
的结果: sh -c- ....
bash-3.2$ sh -c- unoconv -f pdf --stdout /Users/fatimaalves/DEV/xx/_input/sample.doc | pdftotext -layout -enc UTF-8 - -
sh: --: invalid option
Usage: sh [GNU long option] [option] ...
sh [GNU long option] [option] script-file ...
GNU long options:
--debug …Run Code Online (Sandbox Code Playgroud) 对于以下片段:
outer_func(State) ->
spawn(fun()-> do_something(State) end).
Run Code Online (Sandbox Code Playgroud)
是否将State其共享或深复制到生成的进程堆?
执行与执行的命令后execSync,sh我注意到以下内容:
spawnSync /bin/sh ENOENT
bin 当前已添加到 PATH。
有什么想法吗?
我尝试生成子进程 - vvp (https://linux.die.net/man/1/vvp)。在某个时间,我需要发送CTRL+C到该进程。我预计模拟会被中断,并且我会收到交互式提示。之后我可以通过向子进程发送命令来继续模拟。所以,我尝试了这样的事情:
var child = require('child_process');
var fs = require('fs');
var vcdGen = child.spawn('vvp', ['qqq'], {});
vcdGen.stdout.on('data', function(data) {
console.log(data.toString())
});
setTimeout(function() {
vcdGen.kill('SIGINT');
}, 400);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,子进程被停止。我也尝试vcdGen.stdin.write('\x03')过,vcdGen.kill('SIGINT');但它不起作用。
也许是因为Windows?有什么方法可以实现与我在 cmd 中获得的相同的行为吗?
我正在从我的快速脚本中生成命令,但我需要区分错误和成功。我正在寻找一个child.on('success'...)功能,但它不存在。
这段代码(当然)不起作用。
app.get(myID+'/test/', function(req, res){
var child = spawn('lbg');
child.on('error', function(err) {
console.log('Failed to start child process.');
res.status(404).send("Cannot send test command");
return;
});
res.json({statusMessage: "OK", statusCode: "200"});
});
Run Code Online (Sandbox Code Playgroud)
换句话说,我只需要触发正确的响应,而不是两者都触发!
我想创建一个 rust 程序,它通过管道从外部程序输入并通过管道吐到另一个外部程序,就像三明治一样。更具体地说,我尝试创建的玩具模型是“gzip -cd input | rust | gzip -c -> output”。
基本上,我知道如何做第一部分(管道输入),例如:
let child = match Command::new("zcat")
.args(&["input"])
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn();
let filehand = BufReader::new(child.stdout.unwrap());
for line in filehand.lines() {
...
}
Run Code Online (Sandbox Code Playgroud)
但是我被困在第二部分,也不知道如何将两者拼凑起来。我知道 Perl 可以以优雅的方式做到这一点:
open filehand_in, "gzip -cd input |";
open filehand_out, "| gzip -c - > output";
while ( <filehand_in> ) {
# processing;
print filehand_out ...;
}
Run Code Online (Sandbox Code Playgroud)
我所说的优雅是指进出的外部处理对主程序是透明的:您只需要将第一个管道视为标准输入,将第二个管道视为标准输出,无需再担心。我想知道是否有人知道如何在 Rust 中实现类似的实现,谢谢。