如何从Python创建非阻塞命名管道?

时间:2018-04-26 09:57:40

标签: python visual-studio windows-7 named-pipes pywin32

在Windows 7下,我有一个Python 3应用程序,它通过命名管道与可执行文件进行通信,使用Visual Studio以C语言编写。在Python中我使用的是win32pipe包。

我观察到来自管道的可执行文件读取阻塞:

readResult = ReadFile(plafParams->hrpipe_p, &byte, 1, &dwBytesTransferred, &overlap);

打开管道:

...
...
overlap.hEvent = NULL;
overlap.Offset = 0;
overlap.OffsetHigh = 0;
overlap.Pointer = NULL;
...
...
plafParams->hwpipe_p = CreateFile(pipeName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, &overlap);

overlap是一个静态全局变量,如上所述初始化。

OVERLAPPED overlap;

在Python中,管道打开如下:

self._rpipe = win32pipe.CreateNamedPipe(r'\\.\pipe\wskr_%s'%(self._comport,), win32pipe.PIPE_ACCESS_DUPLEX | win32file.FILE_FLAG_OVERLAPPED , 0, 1, 256, 256, 0, None)

等待连接到管道后,我写信给它

win32file.WriteFile(self._rpipe, bytes)

它工作正常,我可以毫无问题地获取可执行文件中的已发送字节,但我希望有一个非阻塞读取。因此我用win32file.FILE_FLAG_OVERLAPPED打开了管道,但这没有用......

我很感激任何提示; - )

0 个答案:

没有答案