python多处理代理和udp监听器

时间:2015-06-20 15:11:14

标签: python parallel-processing udp multiprocessing listener

我有一个使用thul mulitproccessing库创建多个客户端(来自客户端类)的主类(服务器需要更好的术语)。

class mantransact:
def __init__(self,runMode,f_xml):  
    #call the build nodes function
    self.buildNodes()
    sockLisProcess = multiprocessing.Process(target=self.sockListener())
    sockLisProcess.start()
    self.initiateTransactions()

def buildNodes(self,):
    n_source = self.f_xml.getElement("nodeSource")
    self.log.addToLog ("node source is - %s" % n_source)
    self.n_file = load_source.load_source(n_source,"csv")
    #remove header from node list
    del self.n_file.data_list[0]    
    self.nodes = [self.mkproc(node, l) for l in self.n_file.data_list]
    self.log.addToLog(self.nodes)

def mkproc(self, func, line):
    l = "-".join(line)
    p = multiprocessing.Process(target=func, args=(l, self.f_xml))
    p.start()
    return (line[0], p)    

def sockListener(self):
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_addresss = ('localhost',10099)
    self.sock.bind(server_addresss)
    while True:
        self.log.addToLog("server is waitin")
        data, address = self.sock.recvfrom(1024) 
        self.log.addToLog(data, address)   

def sockSender(self,client_address,d):
    self.sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    self.sock2.bind(('localhost',10098))
    recip = ('localhost',int(client_address))
    self.sock2.sendto(str(d),recip)
    self.sock2.close() 

def initiateTransactions(self,):
    #loop through transaction and then loop node list to match at match transmit transaction         
    #using UDP ports
    for t in self.t_file.data_list:
        for n in self.nodes:
            if t[0] == n[0]:
                for l in self.n_file.data_list:
                    if l[0] == n[0]:
                        self.log.addToLog ("gonna transmit UDP transaction to node - %s" % n[0])
                        client_address = l[1]
                        pip = n[2]
                        t.insert(0, "nTransaction")
                        self.sockSender(client_address, t)       

我正在尝试在客户端和节点上创建UDP侦听器:

class node:
def __init__(self,child_conn, line, f_xml):
   l = line.split("-")
   """extract calues from array and use f_xml for config"""
   self.proofProcess = multiprocessing.Process(target=self.runProof(self.waitingTransactions))
    self.proofProcess.start()
   self.listenProcess = Multiprocessing.Process(target=self.udpListener())
   self.listenProcess.start()

def udpListener(self):
    lsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    lsock.bind(("localhost",int(self.IP)))
    while 1 ==1:
        data, addr = lsock.recvfrom(1024)
        print ("received message", data)
        """do some things with data"""

我有两个问题:

1与我的服务器我希望我的代码启动这些进程,然后继续实例化或执行其他任务,但代码只是挂起等待侦听器接收数据包。我错误地实例化处理:

2与我的客户一起执行任务来解决问题,并且在该任务完成之前不启动监听器。他们不能同时开始他们的任务吗?如果另一个clietn首先解决它,然后它将从服务器接收新任务,那么监听器意味着中断计算

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。

将多处理元素放在一个单独的过程中:

def loopProcesses(self,procedureName):

    processX = multiprocessing.Process(target=procedureName)
    processX.start()
    return processX

将要使用的进程的名称放入循环的数组中以调用loopProcesses()进程并行启动两个进程。

m_processes = [self.sockListener(), self.initiateTransactions()]
l_processes = [self.loopProcesses(mp) for mp in m_processes]

答案 1 :(得分:0)

上述方法不起作用,因为被调用的函数在找到许多解决方案之前都处于连续循环中。调用第一个函数时会出现问题,它将在没有启动命令的情况下启动。我后来发现我必须在不使用'()'然后功能将等待。修改后的代码是:

p = [multiprocessing.Process(target=self.sockListener),multiprocessing.Process(target=self.initiateTransactions)]
for prc in p:
    prc.start()

经过大量的搜索后我发现了这个问题,并发现了这个问题:Socketserver multiprocessing.Process is starting without calling start()