从vibe.d同步执行std.process有时会静默挂起服务器

时间:2015-06-15 09:00:37

标签: d vibed

我为vibe.d编写了一个clang-format网络用户界面,当使用this input时,使用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);
}

这可能不应该以阻塞的方式完成,但我不知道如何异步地完成它。我已尝试在runTask中包装函数的内容,但我无法找到正确调用它的方法。

如何让它可靠?

1 个答案:

答案 0 :(得分:1)

您可能在没有阅读stdin的情况下向该程序stdout写了太多数据。由于管道缓冲区大小有限,这会导致执行的程序在写入stdout时阻止,从而导致程序在写入stdin时阻止。

解决方案是在编写数据时读取数据。一种简单的方法是创建第二个线程来读取数据,而主线程则写入它。

相关问题