7

我为 编写了一个vibe.dweb-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);
}

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

我怎样才能使它可靠?

4

1 回答 1

1

stdin您可能在没有读取程序的情况下向程序写入了太多数据stdout。由于管道缓冲区的大小是有限的,这会导致执行的程序在写入时阻塞stdout,这反过来又会导致您的程序在写入时阻塞stdin

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

于 2015-06-15T09:04:44.583 回答