我有一个带有主循环的命名管道服务器,我正在从管道中读取数据:
while True:
bytes_available = DWORD()
ret_code = windll.kernel32.PeekNamedPipe(pipe, None, 0, None, byref(bytes_available))
if ret_code == 0 and windll.kernel32.GetLastError() == ERROR_BROKEN_PIPE:
reconnect()
buf = create_string_buffer(bytes_available.value)
ret_code = windll.kernel32.ReadFile(pipe, byref(buf), bytes_available.value, None, overlapped_struct_ptr)
if ret_code != 0 and bytes_available.value > 0:
# process(buf.raw)
pass
elif ret_code == 0 and windll.kernel32.GetLastError() == ERROR_BROKEN_PIPE:
reconnect()
windll.kernel32.GetQueuedCompletionStatus(iocp, byref(bytes_transferred), byref(completion_key),
byref(overlapped_struct_ptr), INFINITE)
有时它会在create_string_buffer()
步骤崩溃,并显示以下消息:
处理完成,退出代码为-1073741819(0xC0000005)
这对我来说很奇怪,因为如果我用create_string_buffer(bytes_available.value)
替换(c_char*bytes_available.value)()
,一切正常。如果我们看一下create_string_buffer()
实现,我们会看到它以相同的方式分配缓冲区:
def create_string_buffer(init, size=None):
"""create_string_buffer(aString) -> character array
create_string_buffer(anInteger) -> character array
create_string_buffer(aString, anInteger) -> character array
"""
if isinstance(init, (str, unicode)):
if size is None:
size = len(init)+1
buftype = c_char * size
buf = buftype()
buf.value = init
return buf
elif isinstance(init, (int, long)):
buftype = c_char * init
buf = buftype()
return buf
raise TypeError(init)
那为什么会失败呢?