def __init__(self,frontAddress,brokerID,userID,password): """ 1.创建ctp转换器进程 2.创建和ctp通讯进程的通讯管道 3.测试ctp连接是否正常 参数: frontAddress ctp服务器地址 brokerID 代理商Id userID 用户Id password 密码 """ # 创建临时工作目录 self.workdir = tempfile.mkdtemp() # 为ctp md转换器分配通讯管道地址 self.requestPipe = mallocIpcAddress() self.pushbackPipe = mallocIpcAddress() self.publishPipe = mallocIpcAddress() # 构造调用命令 commandLine = ['md', '--FrontAddress',frontAddress, '--BrokerID',brokerID, '--UserID',userID, '--Password', password, '--RequestPipe', self.requestPipe, '--PushbackPipe', self.pushbackPipe, '--PublishPipe', self.publishPipe, '--loyalty' ] # 创建转换器子进程 fileOutput = os.path.join(self.workdir,'md.log') mdStdout = open(fileOutput, 'w') self.__process = subprocess.Popen(commandLine,stdout=mdStdout,cwd=self.workdir) # 创建接受行情数据的管道 global context self.context = context # 创建请求通讯管道 request = context.socket(zmq.REQ) request.connect(self.requestPipe) request.setsockopt(zmq.LINGER,0) self.request = request #创建订阅管道 publish = context.socket(zmq.SUB) publish.connect(self.publishPipe) publish.setsockopt(zmq.SUBSCRIBE, ''); self.publish = publish
def __init__(self,mdConverter,callbackManager): """ """ # 绑定协议管理器和回调管理器实例 self.__mdConverter = mdConverter self.__callbackManager = callbackManager # 分配通讯管道 self.controlPipe = mallocIpcAddress() # 创建线程控制通讯管道 global context request = context.socket(zmq.DEALER) request.connect(self.controlPipe) request.setsockopt(zmq.LINGER,0) self.request = request response = context.socket(zmq.ROUTER) response.bind(self.controlPipe) self.response = response # 创建线程并启动 thread = threading.Thread(target=self.__threadFunction) thread.daemon = True thread.start() self.__thread = thread