我正在将Windows与Python 3.7配合使用,并且尝试在python进程之间异步共享数据(仅字符串)。其中一个无限期运行(接收者),另一个可能在任何时候开始发送一些数据然后结束(发送者)。我正在尝试为此使用命名管道。
当它们同步运行时,我设法做到了(接收者在阻塞的管道上等待直到他获取数据),但是接收者还有其他事情要做,理想情况下,他不应该一直等待。而且在某个时候可能还会有第二个发件人,因此管道阻塞不是很好。
接收方的代码为:
import os
import time
import sys
import win32pipe, win32file, pywintypes
pipe_name = r'\\.\pipe\mypipe'
pipe = win32pipe.CreateNamedPipe(
pipe_name,
win32pipe.PIPE_ACCESS_DUPLEX | win32file.FILE_FLAG_OVERLAPPED, # open mode
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT, # pipe mode
1, 65536, 65536, # max instances, out buffer size, in buffer size
0, # timeout
None)
while 1:
print("doing my other stuff")
try:
win32pipe.ConnectNamedPipe(pipe, pywintypes.OVERLAPPED())
except pywintypes.error as e:
if e.winerror == 232: #disconnected pipe
win32pipe.DisconnectNamedPipe(pipe)
print("Disconnecting pipe")
else:
print(e)
try:
retval, msg = win32file.ReadFile(pipe, 0, pywintypes.OVERLAPPED())
print(msg)
except pywintypes.error as e:
if e.winerror == 536: #Wating for connection
print("waiting")
elif e.winerror == 233: #no process on other side
continue
time.sleep(1)
发件人的代码为:
import os
import time
import sys
import win32pipe, win32file, pywintypes
pipe_name = r'\\.\pipe\mypipe'
for x in range(5):
handle = win32file.CreateFile(
pipe_name,
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
0,
None,
win32file.OPEN_EXISTING,
win32file.FILE_FLAG_OVERLAPPED,
None
)
res = win32pipe.SetNamedPipeHandleState(handle, win32pipe.PIPE_READMODE_MESSAGE, None, None)
print(f"sending {x}")
win32file.WriteFile(handle, str.encode(f"hello world {x}"))
win32file.CloseHandle(handle)
time.sleep(2)
现在两者都可以运行并具有一些连接,但是我无法真正获取数据。如果发送了邮件,接收方可以做其他事情,并断开连接并重新打开管道,但是msg
最终是空的。如果我在调试器中将其停止并发送某些内容,则msg
的值将获得“ 0x0处的内存....”,我将其解释为某种指针,但是正如您probalby注意到的那样,管道有限。
Here我发现了一个同步管有效的很好的例子。我将管道的创建更改为接收方,但这并不难。我找到了一些异步(重叠)管道here的示例,这些示例也很棒,但让我面临了我现在面临的问题。
从重叠的管道中读取数据仍然是win32file.ReadFile
的一项任务,还是我想念的其他东西?
非常感谢您!
答案 0 :(得分:1)
发布者的代码和接受的答案无法处理ERROR_IO_PENDING。不幸的是,我在其他任何地方都找不到很好的Python示例,因此我花了一些时间进行研究。
根据您的环境进行修改,连接到管道服务器,然后调用receive()。它将从管道返回完整的消息,或者无。
如果要观看代码的工作原理,请将read_buf的大小设置为一个较小的值(例如64个字节),并添加一些日志记录语句。
对于文件和套接字,相同的代码应该可以很好地工作(经过轻度修改)。
import win32file
import win32pipe
import winerror
import pywintypes
class pipe_comms:
def __init__(self):
self.pipe_open = False
self.pipe_handle = None
self.pipe_name = r”\\.\pipe\your_pipe”
self.read_msg = None
self.overlapped = None
self.read_buf = win32file.AllocateReadBuffer(4096)
def connect(self):
if not self.pipe_open:
try:
self.pipe_handle = win32file.CreateFile(self.pipe_name
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
0, None, win32file.OPEN_EXISTING,
win32file.FILE_FLAG_OVERLAPPED, None)
win32pipe.SetNamedPipeHandleState(self.pipe_handle,
win32pipe.PIPE_READMODE_MESSAGE, None, None)
self.pipe_open = True
self.read_msg = None
except pywintypes.error as e:
self.handle_error(e.args[0])
return False
return True
def receive(self):
try:
result = self.receive_overlapped()
if result == winerror.ERROR_SUCCESS:
fullMsg = self.read_msg
self.read_msg = None
return fullMsg
elif result == winerror.ERROR_IO_PENDING:
return None
else:
self.handle_error(result)
except pywintypes.error as e:
self.handle_error(e.args[0])
return None
def receive_overlapped(self):
if self.overlapped:
try:
bytes_read = win32file.GetOverlappedResult(self.pipe_handle, self.overlapped, 0)
self.read_msg += bytes(self.read_buf[:bytes_read])
self.overlapped = None
return winerror.ERROR_SUCCESS
except pywintypes.error as e:
result = e.args[0]
if result = winerror.ERROR_MORE_DATA:
bytes_read = len(self.read_buf)
self.read_msg += bytes(self.read_buf[:bytes_read])
# fall thru to issue new ReadFile
else:
# ERROR_IO_PENDING is normal, anything else is not
return result
else:
self.read_msg = bytes()
self.overlapped = pywintypes.OVERLAPPED()
result, data = win32file.ReadFile(self.pipe_handle, self.read_buf, self.overlapped)
while True:
if result = winerror.ERROR_MORE_DATA:
bytes_read = len(pipe_data)
self.read_msg = bytes(data[:bytes_read])
result, data = win32file.ReadFile(self.pipe_handle, self.read_buf, self.overlapped)
continue
elif result == winerror.ERROR_SUCCESS:
bytes_read = win32file.GetOverlappedResult(self.pipe_handle, self.overlapped, 0)
self.read_msg = bytes(data[:bytes_read])
self.overlapped = None
return result
def handle_error(self, result):
reset_pipe = False
if result == winerror.ERROR_BROKEN_PIPE:
win32pipe.DisconnectNamedPipe(self.pipe_handle)
reset_pipe = True
elif result == winerror.ERROR_NO_DATA:
reset_pipe = True
if reset_pipe:
self.pipe_handle = None
self.pipe_open = False
self.read_msg = None
self.overlapped = None
答案 1 :(得分:0)
我找到了解决方案,并希望分享它,以防万一其他人对这个问题之以鼻。事实证明,msg
是一个“ memoryview”对象,它的数据可以通过bytes(msg)
公开。{p}
我的ReadFile
命令也存在问题,因为缓冲区必须大于0才能实现任何目的。现在,它逐个读取每个字节,并将它们加到一个字符串中。这在性能上可能不是很好,但是它对我有用,并且解决了如果消息短于缓冲区长度则必须削减结尾的问题。
msg = ''
rtnvalue, data = win32file.ReadFile(pipe, 1, pywintypes.OVERLAPPED())
while rtnvalue == 234:
msg = msg + bytes(data).decode('ASCII')
rtnvalue, data = win32file.ReadFile(pipe, 1, pywintypes.OVERLAPPED())
if rtnvalue == 0: #end of stream is reached
msg = msg + bytes(data).decode('ASCII')
return msg