我正在开发一个带有.Net(C++)团队的应用程序,并提供一个COM接口来与python和其他语言进行交互.
我们发现通过COM推送数据的速度非常慢.
我考虑了几种选择:
将数据转储到文件并通过com发送文件路径
通过mmap共享内存?
直接通过套接字流数据?
根据您的经验,传递数据的最佳方式是什么?
保持Windows进程间通信机制,我们有使用Windows命名管道的积极经验.使用Windows重叠IO和pywin32中的win32pipe
模块.
你可以在Python Programming On Win32一书中学到很多关于win32和python的知识.
发送部分只是写入r'\\.\pipe\mypipe'
.
listener(ovpipe
)对象包含一个事件句柄,并且等待包含可能的其他事件的消息涉及调用win32event.WaitForMultipleObjects
.
rc = win32event.WaitForMultipleObjects( eventlist, # Objects to wait for. 0, # Wait for one object timeout) # timeout in milli-seconds.
这是python重叠监听器类的一部分:
import win32event import pywintypes import win32file import win32pipe class ovpipe: "Overlapped I/O named pipe class" def __init__(self): self.over=pywintypes.OVERLAPPED() evt=win32event.CreateEvent(None,1,0,None) self.over.hEvent=evt self.pname='mypipe' self.hpipe = win32pipe.CreateNamedPipe( r'\\.\pipe\mypipe', # pipe name win32pipe.PIPE_ACCESS_DUPLEX| # read/write access win32file.FILE_FLAG_OVERLAPPED, win32pipe.PIPE_TYPE_MESSAGE| # message-type pipe win32pipe.PIPE_WAIT, # blocking mode 1, # number of instances 512, # output buffer size 512, # input buffer size 2000, # client time-out None) # no security attributes self.buffer = win32file.AllocateReadBuffer(512) self.state='noconnected' self.chstate() def execmsg(self): "Translate the received message" pass def chstate(self): "Change the state of the pipe depending on current state" if self.state=='noconnected': win32pipe.ConnectNamedPipe(self.hpipe,self.over) self.state='connectwait' return -6 elif self.state=='connectwait': j,self.strbuf=win32file.ReadFile(self.hpipe,self.buffer,self.over) self.state='readwait' return -6 elif self.state=='readwait': size=win32file.GetOverlappedResult(self.hpipe,self.over,1) self.msg=self.strbuf[:size] ret=self.execmsg() self.state = 'noconnected' win32pipe.DisconnectNamedPipe(self.hpipe) return ret