开发者

C# or Python Pipe Blocked

开发者 https://www.devze.com 2023-02-01 03:42 出处:网络
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 tha

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;
usi开发者_高级运维ng 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);
            }
        }
    }
}


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.


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

Maybe it will help.


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消