1

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

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

4

2 回答 2

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"
于 2013-03-19T08:53:29.910 回答
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

因此,如果客户端连接到服务器,它将启动一个新线程。setupand 函数将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
于 2013-03-14T07:35:54.680 回答