Skip to content

Instantly share code, notes, and snippets.

@denik
Created June 5, 2011 09:31
Show Gist options
  • Save denik/1008826 to your computer and use it in GitHub Desktop.
Save denik/1008826 to your computer and use it in GitHub Desktop.
class for managing multiple servers in gevent
# Class for managing multiple servers or anything with start() and stop() methods
class ServerRack(object):
def __init__(self, servers):
self.servers = servers
def start(self):
started = []
try:
for server in self.servers[:]:
server.start()
started.append(server)
name = getattr(server, 'name', None) or server.__class__.__name__ or 'Server'
log('%s started on %s', name, server.address)
except:
self.stop(started)
raise
def stop(self, servers=None):
if servers is None:
servers = self.servers[:]
for server in servers:
try:
server.stop()
except:
if hasattr(server, 'loop'): # gevent >= 1.0
server.loop.handle_error(server.stop, *sys.exc_info())
else: # gevent <= 0.13
import traceback
traceback.print_exc()
# example: run WSGI app on HTTP and HTTPS
rack = ServerRack([WSGIServer(('', 80), application)),
WSGIServer(('', 443), application, keyfile=keyfile, certfile=certfile])
rack.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment