Named Pipes between C# and Python
Posted on Thu 13 December 2012 in blog
There's a lot of over-complicated information on the internet for communicating between a C# process and a Python process using named pipes on Windows. I'll start with the code:
In this example, I implement a very simple protocol, where every "message" is a
4-byte integer (UInt32
in C#, I
(un)pack format in Python), which indicates
the length of the string that follows. The string is ASCII. Important things to
note here:
- Python
- The third parameter to
open()
means "unbuffered". Otherwise, it will default to line-buffered, which means it will wait for a newline character before actually sending it through the pipe. - I'm not sure why, but omitting the
seek(0)
will cause an IOError #0. I was clued to this by a StackOverflow question.
- The third parameter to
References
- .NET
NamedPipeServerStream
class on MSDN - Python
open()
method on Python.org