views:

335

answers:

2

I'm trying to create a two-way communication channel between two programs (one in Python and another in C#)

When I create a named pipe between two C# programs or two Python programs, everything is OK, but when I try to (for example) connect to the C# server from Python code, it does not work:

C# code:

NamedPipeServerStream server = new NamedPipeServerStream("Demo", PipeDirection.InOut,100,PipeTransmissionMode.Byte,PipeOptions.None,4096,4096)
  1. If I use win32pipe in Python, code blocks on ConnectNamedPipe (it never returns)

    p = win32pipe.CreateNamedPipe( r'\.\pipe\Demo', win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_WAIT, 1, 65536, 65536, 300, None) win32pipe.ConnectNamedPipe(p)

  2. If I use open function, it just establishes a connection, but nothing occurs:

    open( '\\.\pipe\Demo', 'r+b' )

    Now if I close the Python program, C# server receives just one data item from Python and a System.IO.IOException raises with "Pipe is broken" message

Am I doing anything wrong ?

A: 

I think you are meant to use win32pipe.popen, not open.

Also try: pipe.flush(), pipe.read(), and time.sleep(0.01). Sometimes IPC takes a while to sync up.

I don't really know, that's my experience with the subprocess pipes. win32pipe might be different.

wisty
I used win32pipe.popen. Nothing changedHave you managed to create a named pipe between Python and .NET ?I don't know why win32pipe.ConnectNamedPipe(p) never returns
Mehdi Asgari
I also used win32file, but now the connection establishes but WriteFile does not work (Yes, I've also flushed the buffer)
Mehdi Asgari
+1  A: 

OK, I fixed the problem. I should seek to position 0 of buffer.

My Python code:

    win32file.WriteFile(CLIENT_PIPE,"%d\r\n"%i ,None)
    win32file.FlushFileBuffers(CLIENT_PIPE)
    win32file.SetFilePointer(CLIENT_PIPE,0,win32file.FILE_BEGIN)
    i,s = win32file.ReadFile(CLIENT_PIPE,10,None)
Mehdi Asgari