Skip to content

Instantly share code, notes, and snippets.

@lukemarsden
Created September 29, 2011 19:17
Show Gist options
  • Save lukemarsden/1251662 to your computer and use it in GitHub Desktop.
Save lukemarsden/1251662 to your computer and use it in GitHub Desktop.
pid = os.fork()
if pid != 0:
# Make the original process the master
masterService = StreamServerEndpointService(
TCP4ServerEndpoint(reactor, 9881),
pb.PBServerFactory(AwesomeProxyMaster()))
masterService.setServiceParent(application)
# Name the master nicely (avoid backward compatibility with restarts/upgrades)
HybridUtils.setproctitle('AwesomeProxy')
# Notice problems blocking the reactor
import btt
btt.BigTimesliceTimer(logName='ap').start(precision=1.0)
else:
# A child for each proxy type
for (factory, string) in PROXIES:
print "Worker for", string, "has pid", os.getpid()
pid = os.fork()
if pid == 0:
# We're the next worker;
import btt
btt.BigTimesliceTimer(logName='ap-%s' % (string,)).start(precision=1.0)
HybridUtils.setproctitle('AwesomeProxy-%s' % (string,))
# In this new child, start one proxy
# Do setup of port bindings etc
controller = Controller()
connect_proxy(factory, string, delayed_recurse=True)
# TODO This services tries to connect to the master, but the master
# might not have had time to start listening yet. It's a race
# between which process gets the reactor going first.
service = MasterCommunicationService(string, controller,
TCP4ClientEndpoint(reactor, '127.0.0.1', 9881))
service.setServiceParent(application)
# Don't do any more work from this PROXIES loop in the child, either.
break
else:
# We're the next child, about to go round the loop again
print "Next child in the loop", os.getpid()
# TODO make the one lingering process here (which is not doing any work) exit
"""
Result:
Original master pid 74063
Worker for https has pid 74074
Next child in the loop 74074
Worker for http has pid 74074
Next child in the loop 74074
Worker for pop3 has pid 74074
Next child in the loop 74074
Worker for mysql has pid 74074
Next child in the loop 74074
Worker for smtp has pid 74074
Next child in the loop 74074
Worker for ftp has pid 74074
Next child in the loop 74074
Started
Started
Started
Worker for imap has pid 74074
Next child in the loop 74074
Worker for https-internal has pid 74074
Next child in the loop 74074
Worker for ssh has pid 74074
Next child in the loop 74074
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment