123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- # -*- coding:utf-8 -*-
- import sys,os,time
- reload(sys)
- sys.setdefaultencoding("utf-8")
- from multiprocessing import Process, Queue, Pipe
- from multiprocessing.connection import Listener,Client
- import json
- class ProcessSync():
- HEAD = "[ProcessCMD]"
- def __init__(self):
- pass
- def notifyMessage(self, msg):
- sendMsg = ProcessSync.HEAD + msg
- print sendMsg
- class QueueBinder():
- def __init__(self, sender, receiver):
- self.sender = sender
- self.receiver = receiver
- def setQueue(self, sendQue, recvQue):
- self.sender = sendQue
- self.receiver = recvQue
- """
- 参数:action:字符串
- data:字典,存放基本数据类型
- """
- def sendData(self, action, data):
- packet = [action,data]
- self.sender.put(packet)
- """
- 返回值:数组[action, data]
- data:字典,存放基本数据类型
- """
- def accept(self):
- return self.receiver.get()
- queBinder = None
- def getQueueBinder(sendQue, recvQue):
- global queBinder
- if (queBinder == None):
- queBinder = QueueBinder(sendQue, recvQue)
- else:
- queBinder.setQueue(sendQue,recvQue)
- return queBinder
- def testP1():
- binder = HttpConsoleBinder()
- binder.initConsoleListener()
- print "Wait http"
- action,data = binder.acceptHttp()
- print "testP1,action:",action,";data:",data
- print "Send to http"
- binder.sendToHttp("test", {"keyP1": "valueP1"})
- def testP2():
- binder = HttpConsoleBinder()
- binder.initHttpListener()
- print "Send to console"
- binder.sendToConsole("test", {"keyP2":"valueP2"})
- print "Wait console"
- action,data = binder.acceptConsole()
- print "testP2,action:",action,";data:",data
- class HttpConsoleBinder():
- HttpServer_Port = 6000
- Console_Port = HttpServer_Port + 1
- def __init__(self):
- self.httpAddress = ("localhost", HttpConsoleBinder.HttpServer_Port)
- self.consoleAddress = ("localhost", HttpConsoleBinder.Console_Port)
- def initHttpListener(self):
- self.httpListener = Listener(self.httpAddress, authkey="password")
- def initConsoleListener(self):
- self.consoleListener = Listener(self.consoleAddress, authkey="password")
- def sendToHttp(self,action, data):
- self.httpClient = Client(self.httpAddress, authkey="password")
- data["action"] = action
- jsonStr = json.dumps(data)
- try:
- self.httpClient.send(jsonStr)
- except Exception,e:
- print e
- finally:
- self.httpClient.close()
- def sendToConsole(self, action, data):
- self.consoleClient = Client(self.consoleAddress, authkey="password")
- data["action"] = action
- jsonStr = json.dumps(data)
- try:
- self.consoleClient.send(jsonStr)
- except Exception,e:
- print e
- finally:
- self.consoleClient.close()
- def acceptHttp(self):
- conn = self.consoleListener.accept()
- data = conn.recv()
- dataDict = json.loads(data)
- action = dataDict["action"]
- conn.close()
- return action,dataDict
- def acceptConsole(self):
- conn = self.httpListener.accept()
- data = conn.recv()
- dataDict = json.loads(data)
- action = dataDict["action"]
- conn.close()
- return action,dataDict
- if __name__ == "__main__":
- p1 = Process(target=testP1, args=())
- p1.start()
- time.sleep(3)
- p2 = Process(target=testP2, args=())
- p2.start()
|