public
Last active

class for managing multiple servers in gevent

  • Download Gist
ServerRack.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# 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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.