Skip to content

Instantly share code, notes, and snippets.

@jianingy
Created April 5, 2013 08:02
Show Gist options
  • Save jianingy/5317451 to your computer and use it in GitHub Desktop.
Save jianingy/5317451 to your computer and use it in GitHub Desktop.
A method for starting multiple twisted instance
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# filename : almarctl.py
# created at : 2013-04-04 21:27:12
# author : Jianing Yang <jianingy.yang AT gmail DOT com>
__author__ = 'Jianing Yang <jianingy.yang AT gmail DOT com>'
from os import environ
from sys import argv as ARGV, executable
from socket import AF_INET
from twisted.internet import reactor
from twisted.python import usage
from almar import _init as almar_init
from almar.global_config import GlobalConfig
class Options(usage.Options):
optParameters = [
['config', 'c', 'etc/production.yaml', 'Path to top configuration.'],
['port', 'p', None, 'Port to listen.'],
['fd', '', None, 'fd'],
['id', '', None, 'worker identitifer'],
['process', 'n', 4, 'num of processes'],
]
optFlags = [
["proxy", "x", "Start in proxy mode"],
]
def main(options, fd=None):
site = almar_init(options['config'])
if options['proxy']:
site = almar_init(options['config'], mode='proxy')
g = GlobalConfig()
portno = int(options["port"] or g.proxy.port)
else:
site = almar_init(options['config'], mode='normal')
g = GlobalConfig()
portno = int(options["port"] or g.server.port)
if fd is None:
# Create a new listening port and several other processes to help out.
port = reactor.listenTCP(portno, site)
for i in range(int(options['process'])):
new_args = [executable]
new_args.extend(ARGV)
new_args.extend(['--fd', str(port.fileno()), '--id', str(i)])
fds = {0: 0, 1: 1, 2: 2, port.fileno(): port.fileno()}
reactor.spawnProcess(None, executable, new_args,
childFDs=fds, env=environ)
else:
# Another process created the port, just start listening on it.
from procname import setprocname
setprocname('almar worker #%s%-100s' % (options['id'], ''))
port = reactor.adoptStreamPort(fd, AF_INET, site)
reactor.run()
if __name__ == '__main__':
options = Options()
options.parseOptions()
if options['fd']:
main(options, fd=int(options['fd']))
else:
main(options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment