windows C# 和 Python 之间的命名管道

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1749001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:27:13  来源:igfitidea点击:

Named Pipes between C# and Python

c#pythonwindowsnamed-pipes

提问by Mehdi Asgari

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

我正在尝试在两个程序之间创建一个双向通信通道(一个在 Python 中,另一个在 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# 程序或两个 Python 程序之间创建命名管道时,一切正常,但是当我尝试(例如)从 Python 代码连接到 C# 服务器时,它不起作用:

C# code:

C#代码:

NamedPipeServerStream server = new NamedPipeServerStream(
    "Demo", PipeDirection.InOut, 100, PipeTransmissionMode.Byte,
    PipeOptions.None, 4096, 4096)

If I use win32pipein Python, code blocks on ConnectNamedPipe(it never returns)

如果我win32pipe在 Python 中使用,代码块ConnectNamedPipe(它永远不会返回)

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)

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

如果我使用 open 函数,它只是建立一个连接,但没有任何反应:

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.IOExceptionraises with "Pipe is broken" message

现在,如果我关闭 Python 程序,C# 服务器只会从 Python 接收一个数据项,并且System.IO.IOException会引发“管道损坏”消息

Am I doing anything wrong ?

我做错了什么吗?

采纳答案by Mehdi Asgari

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

好的,我解决了问题。我应该寻求定位缓冲区的 0。

My Python code:

我的 Python 代码:

    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)

回答by Pakman

According to MS, ConnectNamedPipeis the "server-side function for accepting a connnection". That's why it never returns - it's waiting for a connection from a client. Here's some sample code showing C# as the server and python as the client:

根据MSConnectNamedPipe是“用于接受连接的服务器端功能”。这就是它永远不会返回的原因——它正在等待来自客户端的连接。下面是一些示例代码,将 C# 显示为服务器,将 python 显示为客户端:

C#:

C#:

NamedPipeServerStream server = new NamedPipeServerStream("Demo");
server.WaitForConnection();

MemoryStream stream = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(stream))
{
    writer.Write("print \"hello\"");
    server.Write(stream.ToArray(), 0, stream.ToArray().Length);
}

stream.Close();
server.Disconnect();
server.Close();

python:

Python:

import win32file
fileHandle = win32file.CreateFile("\\.\pipe\Demo", win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0, None, win32file.OPEN_EXISTING, 0, None)
left, data = win32file.ReadFile(fileHandle, 4096)
print data # prints \rprint "hello"

回答by wisty

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

我认为您打算使用 win32pipe.popen,而不是 open。

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

还可以尝试:pipe.flush()、pipe.read() 和 time.sleep(0.01)。有时 IPC 需要一段时间才能同步。

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

我真的不知道,这是我对子流程管道的经验。win32pipe 可能会有所不同。