多进程扭曲服务

时间:2016-01-14 18:31:18

标签: python service multiprocessing twisted

我几乎阅读了有关使用twisted进行多处理的所有问题,但我想知道如何使用twisted.application.service部署我的TCP应用程序,我有这段代码使用服务部署我的工厂:

from project.application import GameServerFactory
from twisted.application import internet, service

port = 8000
factory = GameServerFactory()

application = service.Application("Game Server")
service = internet.TCPServer(port, factory)
service.setServiceParent(application)

但我想使用我的所有核心(我从另一个问题复制它):

from project.application import GameServerFactory

from os import environ
from sys import argv, executable
from socket import AF_INET
import sys

from twisted.internet import reactor

def main(fd=None):
    factory = GameServerFactory()

    if fd is None:
    # Create a new listening port and several other processes to help out.                                                                     
        port = reactor.listenTCP(8000, factory)
        for i in range(7):
            reactor.spawnProcess(
                    None, executable, [executable, __file__, str(port.fileno())],
                    childFDs={0: 0, 1: 1, 2: 2, port.fileno(): port.fileno()},
                    env=environ)
        #sys.exit()
    else:
        # Another process created the port, just start listening on it.                                                                            
        port = reactor.adoptStreamPort(fd, AF_INET, factory)

    reactor.run()

if __name__ == '__main__':
    if len(argv) == 1:
        main()
    else:
        main(int(argv[1]))

如何更改我的第一个代码以获取输出,如我的第二个代码示例,但作为服务和守护进程?

1 个答案:

答案 0 :(得分:0)

最后我决定使用HAProxy(n)进程扭曲。