如何同时运行2个不同的进程?

时间:2013-03-14 06:57:06

标签: python sockets parallel-processing queue

一个进程是从套接字接收数据然后将其放入队列,另一个进程正在处理排队的数据。如何使它们同时运行?

此套接字是serve_forever,而数据处理仅在队列不为空时才会运行。

2 个答案:

答案 0 :(得分:1)

我有它的工作。我不知道这是否是答案。但它到目前为止工作正常。也许有或者会有错误(希望没有)。欢迎任何改进建议。

import multiprocessing
import socket
from multiprocessing import Process, Queue
import time

def handle(connection, address):
  try:
    while True:
      data = connection.recv(1024)
      if data == "":
        break
      else :
        print "RECEIVE DATA : " + str(data)
        xdata = data.strip()
        xdata = data.split(" ")
        for xd in xdata :
          print "PUT Task : " + str(xd)
          QueueTask.put((xd), block=True, timeout=5)
      connection.sendall(data)
  except:
    print "Problem handling request"
  finally:
    connection.close()

class Server(object):
  def __init__(self, hostname, port):
    self.hostname = hostname
    self.port = port

  def start(self):
    self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.socket.bind((self.hostname, self.port))
    self.socket.listen(1)
    while True:
      conn, address = self.socket.accept()
      process = multiprocessing.Process(target=handle, args=(conn, address))
      process.daemon = True
      process.start()

def f_Processor():
  time.sleep(10)
  print 'PROCESSOR Starting'
  while 1:
    try :
      job = QueueTask.get(True,1)
      print "GET Task : " + str(job)
      time.sleep(5)
    except Exception as err :
      pass
  print 'PROCESSOR Exiting'

if __name__ == "__main__":
  server = Server("localhost", 9999)
  QueueTask = Queue()
  try:
    p = multiprocessing.Process(name='Processing', target=f_Processor)
    p.start()
    server.start()
    p.join()
  except:
    print "Unexpected exception"
  finally:
    for process in multiprocessing.active_children():
      process.terminate()
      process.join()
  print "All done"

答案 1 :(得分:0)

如果您有服务器或客户端应用程序,也取决于它。如果它是服务器而不是你可以使用

    SocketServer.TCPServer.allow_reuse_address = True       

    self.server = TCPFactory( ( HOST, PORT ), TCPRequestHandler, params )                      

    # Start a thread with the server        
    self.server_thread = threading.Thread( target = self.server.serve_forever )


    self.server_thread.setDaemon( True )       
    self.server_thread.start()

class TCPFactory( SocketServer.ThreadingTCPServer ):    


    def __init__( self, server_address, RequestHandlerClass, params ):
        """
        """

        SocketServer.ThreadingTCPServer.__init__( self, server_address, RequestHandlerClass )
        self.patrams = params


class TCPRequestHandler( SocketServer.BaseRequestHandler ):

    def setup( self ):
       print self.server.params
       pass
    def handle( self ):
       pass

因此,如果客户端连接到服务器,它将启动一个新线程。 setup和。{ handler函数将自动调用。 对于另一个线程,您可以使用计时器或其他线程

myt = Timer( 2, chackque, () )
myt.start()

def chackque():
   if not myq.empty():
   #Do what you want

或者只是开始另一个主题:

mythread = threading.Thread( target = chackque, args = ( myargs, ) )        
mythread.setDaemon( True )
mythread.start()

def chackque():
   while True:
       if not myq.empty():
       #Do what you want