目前,我有一个有两个服务器的应用程序:第一个处理订单并单独响应,第二个将结果广播给其他感兴趣的订阅者。它们需要从不同的端口提供服务。我可以 start() 他们两个,但我只能得到一个或另一个到 serve_forever() 因为我读到它是一个阻塞函数。我正在寻找有关如何防止两个服务器退出的想法。缩写代码如下:
def main():
stacklist = []
subslist = []
stacklist.append(CreateStack('stuff'))
subslist.append(Subscription('stuff'))
bcastserver = BroadcastServer(subslist) # creates a new server
tradeserver = TradeServer(stacklist) # creates a new server
bcastserver.start() # start accepting new connections
tradeserver.start() # start accepting new connections
#bcastserver.serve_forever() #if I do it here, the first one...
#tradeserver.serve_forever() #blocks the second one
class TradeServer(StreamServer):
def __init__(self, stacklist):
self.stacklist = stacklist
StreamServer.__init__(self, ('localhost', 12345), self.handle)
#self.serve_forever() #If I put it here in both, neither works
def handle(self, socket, address):
#handler here
class BroadcastServer(StreamServer):
def __init__(self, subslist):
StreamServer.__init__(self, ('localhost', 8000), self.handle)
self.subslist = subslist
#self.serve_forever() #If I put it here in both, neither works
def handle(self, socket, address):
#handler here
也许我只是需要一种方法来阻止两者退出,但我不确定如何。最后,我希望两台服务器都永远监听传入的连接并处理它们。