多进程扭曲服务
multiprocess twisted service
我几乎阅读了所有关于使用 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]))
我如何更改我的第一个代码以获得像我的第二个代码示例一样的输出,但作为服务和守护进程?
最后我决定在 (n) 个 twisted 进程前使用 HAProxy。
我几乎阅读了所有关于使用 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]))
我如何更改我的第一个代码以获得像我的第二个代码示例一样的输出,但作为服务和守护进程?
最后我决定在 (n) 个 twisted 进程前使用 HAProxy。