我有一个与此类似的基于Python的SimpleXMLRPCServer
:
from multiprocessing import Process
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import SocketServer
class RPCThreading(SocketServer.ThreadingMixIn, SimpleXMLRPCServer):
pass
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
def main():
server = RPCThreading(('127.0.0.1', 8000), requestHandler=RequestHandler)
server.register_function(tester1)
server.register_function(tester2)
print("Server running...")
server.serve_forever()
def tester1(id):
p = Process(target=my_func1, args=(id,))
p.start()
return True
def tester2(id):
p = Process(target=my_func2, args=(id,))
p.start()
return True
我想实现一种方法,以跟踪tester1
和tester2
当前正在执行多少个并发进程,如果仍在执行多个(用户定义的)最大并发进程,则将每个队列排队新的请求,并在数字降至阈值以下时执行。
也许每个功能共享一个Pool
?
以下内容似乎可以满足我的要求:
from multiprocessing import Process, JoinableQueue
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import SocketServer
import threading
tester1_queue = JoinableQueue()
tester2_queue = JoinableQueue()
tester1_max_concurrent = 10
tester2_max_concurrent = 10
class RPCThreading(SocketServer.ThreadingMixIn, SimpleXMLRPCServer):
pass
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
def main():
# spin up tester1 queue watcher threads
for i in range(tester1_max_concurrent):
worker = threading.Thread(target=tester1_queue_watcher, args=(tester1_queue,))
worker.daemon = True
worker.start()
# spin up tester2 queue watcher threads
for i in range(tester2_max_concurrent):
worker = threading.Thread(target=tester2_queue_watcher, args=(tester2_queue,))
worker.daemon = True
worker.start()
server = RPCThreading(('127.0.0.1', 8000), requestHandler=RequestHandler)
server.register_function(tester1_handler, 'tester1')
server.register_function(tester2_handler, 'tester2' )
print("Server running...")
server.serve_forever()
def tester1_handler(id):
tester1_queue.put((id,))
return True
def tester1_queue_watcher(q):
while True:
id = q.get()
p = Process(target=tester1, args=(id,))
p.start()
p.join()
q.task_done()
def tester1(id):
# do stuff
def tester2_handler(id):
tester2_queue.put((id,))
return True
def tester2_queue_watcher(q):
while True:
id = q.get()
p = Process(target=tester2, args=(id,))
p.start()
p.join()
q.task_done()
def tester2(id):
# do stuff