我有一个用 python+ctypes 实现的简单命名管道服务器:
pipe = windll.kernel32.CreateNamedPipeA('\\\\.\\pipe\\pipe_name', PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 0, 0, 0, None)
overlapped_struct = OVERLAPPED()
windll.kernel32.ConnectNamedPipe(pipe, byref(overlapped_struct))
iocp = windll.kernel32.CreateIoCompletionPort(pipe, None, WPARAM(707070707), 0)
while True:
bytes_transferred = DWORD()
completion_key = WPARAM()
overlapped_struct_ptr = POINTER(OVERLAPPED)()
windll.kernel32.GetQueuedCompletionStatus(iocp, byref(bytes_transferred), byref(completion_key), byref(overlapped_struct_ptr), INFINITE)
bytes_available = DWORD()
windll.kernel32.PeekNamedPipe(pipe, None, 0, None, byref(bytes_available))
buf = create_string_buffer(bytes_available.value)
ret_code = windll.kernel32.ReadFile(pipe, byref(buf), bytes_available.value, None, overlapped_struct_ptr)
它从外部程序接收数据。我希望GetQueuedCompletionStatus
只有当有东西到达管道时才会返回,但情况并非总是如此。有时,在完成数据包出队后,我可以看到bytes_available == 0
,ret_code == 0
和overlapped_struct.Internal == 256
(我认为这意味着 ERROR_NO_MORE_ITEMS)。
关于为什么会发生的任何想法?