C# 或 Python 管道阻塞

发布于 2024-10-09 10:25:17 字数 2195 浏览 9 评论 0原文

我试图简单地将我的程序(用 C# 编写)的标准输出通过管道传输到另一个程序(例如 python 脚本)。我的代码只是使用 Console.Write() 写入标准输出,当我不进行管道传输时,它工作得很好。当数据从已设置的套接字传入时,listen 命令仅使用 Console.Write() 从单独的线程写入数据。

[有效 - 在收到数据时将数据写入控制台]

myProgram.exe listen

[不起作用 - 没有任何内容写入控制台]

myProgram.exe listen | python filter.py

我不完全确定出了什么问题,并且还没有甚至没有想到解决这个问题的方法。我的猜测是,问题与接收线程以某种方式阻止将标准输出从管道数据传输到另一个进程有关,但我不知道如何测试这一点。我正在寻找任何对问题的实际情况有想法的人,或者有进一步解决此问题的方法的人。如果您需要代码来帮助,我愿意发布代码片段。

如何判断问题出在 C# 还是 Python 中?

编辑:请注意,重定向运算符 (>) 确实有效。所以,myProgram.exe Listen > log.txt 实际上会将正在写入标准输出的数据写入到 log.txt 文件中。我还尝试按照 http://linux.byexamples.com/archives/343/python-handle-string-from-pipelines-and-list-of-param/

[filter.py]

import sys
for line in sys.stdin:
    sys.stdout.write(line)

编辑:控制台反馈

我认为这是值得一提的。 python 脚本正在被调用,但等待 30 秒后(它应该立即开始输出到标准输出),我按 Ctrl + C 停止该进程并得到以下结果。

>> myProgram.exe listen | python filter.py
Traceback (most recent call last):
  File "filter.py", line 2, in 
    for line in sys.stdin:
KeyboardInterrupt
^C

编辑:@Aaronaught

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace echo
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                foreach (string arg in args)
                {
                    Console.Out.WriteLine(arg);
                }
            }
            else
            {
                Thread thread = new Thread(new ThreadStart(Receive));
                thread.Start();
                thread.Join();
            }
        }

        static void Receive()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.Out.WriteLine(i);
            }
        }
    }
}

I'm trying to simply be able to pipe the standard output of my program (written in C#) to another program (e.g. python script). My code simply writes to standard output using Console.Write(), and that works just fine when I'm not piping. The listen command just uses Console.Write() to write data from a separate Thread when it comes in from a socket that is setup.

[Works - writes data to console as it is received]

myProgram.exe listen

[Does Not Work - nothing is written to console]

myProgram.exe listen | python filter.py

I'm not entirely sure what is going wrong, and haven't thought of a way to even troubleshoot this problem. My guess is that the problem has to do with the receiving Thread somehow blocking the standard output from piping data to another process, but I don't know how to test this. I'm looking for anyone who has an idea(s) on what the problem might actually be, or a way to further troubleshoot this. If you need code to help, I'd be willing to post snippets.

How do I tell if the problem lies in C# or in Python?

EDIT: Note that the redirection operator (>) does work though. So, myProgram.exe listen > log.txt does in fact write the data being written to standard out to the log.txt file. I've also tried following the example at http://linux.byexamples.com/archives/343/python-handle-string-from-pipelines-and-list-of-param/.

[filter.py]

import sys
for line in sys.stdin:
    sys.stdout.write(line)

EDIT: Console feedback

I figured this is worth mentioning. The python script is getting called, but after waiting for 30 seconds (it should start outputting to standard out right away) I press Ctrl + C to stop the process and get the following.

>> myProgram.exe listen | python filter.py
Traceback (most recent call last):
  File "filter.py", line 2, in 
    for line in sys.stdin:
KeyboardInterrupt
^C

EDIT: @Aaronaught

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace echo
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                foreach (string arg in args)
                {
                    Console.Out.WriteLine(arg);
                }
            }
            else
            {
                Thread thread = new Thread(new ThreadStart(Receive));
                thread.Start();
                thread.Join();
            }
        }

        static void Receive()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.Out.WriteLine(i);
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ杯具 2024-10-16 10:25:17

您的程序是否在其标准输出上写入换行符或调用刷新?也许它被缓冲了,这就是为什么你看不到任何东西。

Does your program write newlines or call flush on its standard output? Maybe it's buffered and that's why you can't see anything.

荒芜了季节 2024-10-16 10:25:17

您可能需要查看此处并搜索“僵局”。

也许会有帮助。

You may want to look here and search for "deadlock".

Maybe it will help.

烧了回忆取暖 2024-10-16 10:25:17

您的 python 程序需要 2 个 EOF 字符才能终止。试试这个。

import sys

line=sys.stdin.readline()
while len(line):
    sys.stdout.write(line)
    line=sys.stdin.readline()

Your python program requires 2 EOF characters to terminate. Try this.

import sys

line=sys.stdin.readline()
while len(line):
    sys.stdout.write(line)
    line=sys.stdin.readline()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文