2

I'm trying to send data from a python script to my c# application using a standard input stream. I also need to eventually send data back from my c# application to the another python script. I first tried to do this with a UDP Connection which works fine for a couple lines, but I have a fair amount of data to send (a few thousand lines) and a requirement for data integrity which UDP cannot provide. I could also write to a file, but that seems very inefficient.

One last restriction is that while my two applications are related I cannot setup a direct connection between them using something like IronPython as they are both spawned separately by a 3rd party application.

This is what I am currently trying, but it is not working. Similar to this question: Passing data between Python and C# without writing a file

p = subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate(blob)

On my C# side I'm not entirely sure how to read this data, but I've tried using things like a loop around this:

Console.ReadLine()

or getting the standard input and reading from it directly using:

Console.OpenStandardInput();

The current issue is that as soon as I call p.communicate my Python script gets locked and doesn't proceed. If it's waiting for the line to be read, what do I need to do to make it stop waiting? I tried only providing the stdin parameter, but that didn't help.

6
  • How big is blob? Docs say there could be problems with large input. Does it hang when passing small message? Ctrl+D sends "end of input" signal on Unix, Ctrl+Z Enter closes stdin on Windows. Commented Jan 6, 2017 at 23:24
  • I've tested with a single line of a few characters and it still hangs. However I will eventually have a few thousands lines in the blob, is there a better mechanism for this? Commented Jan 6, 2017 at 23:35
  • What do you need is called IPC. I would recommend trying 0MQ, though you should be aware that IPC is generally more complex than working with stdin / stdout. Commented Jan 8, 2017 at 20:06
  • Is this going to be significantly faster then simply reading/writing to/from a file? That is very simple and I currently have it working, I'm not sure how much of an advantage this would be. Any thoughts? Commented Jan 9, 2017 at 3:51
  • That depends too much on applications using IPC and on infrastructure. With some skill one can achieve millions of messages per second passed by ZeroMQ. And slow receiver can ensure bad performance even when taking data from files. Just do a quick smoke test. Real tasks which require outstanding IPC performance are rare. Commented Jan 9, 2017 at 16:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.