我写了一个vibe.d
web-UI clang-format
,当使用LLVM样式时显示此输入时,服务器挂起.
处理POST的代码:
void post(string style, string code)
{
import std.algorithm;
import std.file;
import std.conv;
import std.process;
auto pipes = pipeProcess(["clang-format", "-style="~style], Redirect.stdout | Redirect.stdin);
scope(exit) wait(pipes.pid);
pipes.stdin.write(code);
pipes.stdin.close;
pipes.pid.wait;
code = pipes.stdout.byLine.joiner.to!string;
string selectedStyle = style;
render!("index.dt", styles, code, selectedStyle);
}
Run Code Online (Sandbox Code Playgroud)
这可能不应该以阻塞的方式完成,但我不知道如何异步地执行它.我已经尝试包装函数的内容runTask
,但我找不到正确调用它的方法.
我怎样才能让它可靠?
stdin
您可能在没有读取程序的情况下向程序写入了太多数据stdout
。由于管道缓冲区大小有限,这会导致执行的程序在写入其 时阻塞stdout
,进而导致您的程序在写入其 时阻塞stdin
。
解决方案是在写入数据时读取数据。一种简单的方法是创建第二个线程来读取数据,同时主线程写入数据。