Source code for gevent_tools.service
import gevent
import gevent.baseserver
import gevent.event
import gevent.pool
import gevent.util
READY = True
[docs]class Service(object):
"""Service interface for creating standalone or composable services
This is similar to a subset of the gevent baseserver interface (intentional)
so that you can treat them as children services.
"""
stop_timeout = 1
ready_timeout = 2
def __init__(self):
self._stopped_event = gevent.event.Event()
self._ready_event = gevent.event.Event()
self._children = set()
self._greenlets = gevent.pool.Group()
self._error_handlers = {}
self.started = False
@property
[docs] def ready(self):
"""This property returns whether this service is ready for business"""
return self._ready_event.isSet()
[docs] def _ready(self):
"""Internal convenience function to proclaim readiness"""
self._ready_event.set()
[docs] def add_service(self, service):
"""Add a child service to this service"""
self._children.add(service)
[docs] def remove_service(self, service):
"""Remove a child service from this service"""
self._children.remove(service)
def _wrap_errors(self, func):
"""Wrap a callable for triggering error handlers
This is used by the greenlet spawn methods so you can handle known
exception cases instead of gevent's default behavior of just printing
a stack trace for exceptions running in parallel greenlets.
"""
def wrapped_f(*args, **kwargs):
exceptions = tuple(self._error_handlers.keys())
try:
return func(*args, **kwargs)
except exceptions, exception:
for type in self._error_handlers:
if isinstance(exception, type):
handler, greenlet = self._error_handlers[type]
self._wrap_errors(handler)(exception, greenlet)
return exception
return wrapped_f
[docs] def catch(self, type, handler):
"""Set an error handler for exceptions of `type` raised in greenlets"""
self._error_handlers[type] = (handler, gevent.getcurrent())
[docs] def spawn(self, func, *args, **kwargs):
"""Spawn a greenlet under this service"""
func_wrap = self._wrap_errors(func)
return self._greenlets.spawn(func_wrap, *args, **kwargs)
[docs] def spawn_later(self, seconds, func, *args, **kwargs):
"""Spawn a greenlet in the future under this service"""
group = self._greenlets
func_wrap = self._wrap_errors(func)
g = group.greenlet_class(func_wrap, *args, **kwargs)
g.start_later(seconds)
group.add(g)
return g
[docs] def start(self, block_until_ready=True):
"""Public interface for starting this service and children. By default it blocks until ready."""
assert not self.started, '%s already started' % self.__class__.__name__
self._stopped_event.clear()
self._ready_event.clear()
try:
for child in self._children:
if isinstance(child, Service):
child.start(block_until_ready)
elif isinstance(child, gevent.baseserver.BaseServer):
child.start()
ready = self._start()
if ready:
self._ready_event.set()
elif not ready and block_until_ready is True:
self._ready_event.wait(self.ready_timeout)
self.started = True
except:
self.stop()
raise
[docs] def _start(self):
"""Empty implementation of service start. Implement me!"""
return READY
[docs] def stop(self, timeout=None):
"""Stop this service and child services
If the server uses a pool to spawn the requests, then :meth:`stop` also waits
for all the handlers to exit. If there are still handlers executing after *timeout*
has expired (default 1 second), then the currently running handlers in the pool are killed."""
if gevent.getcurrent() in self._greenlets:
return gevent.spawn(self.stop)
self.started = False
try:
for child in self._children:
child.stop()
self._stop()
finally:
if timeout is None:
timeout = self.stop_timeout
if self._greenlets:
self._greenlets.join(timeout=timeout)
self._greenlets.kill(block=True, timeout=1)
self._ready_event.clear()
self._stopped_event.set()
[docs] def _stop(self):
"""Empty implementation of service start. Implement me!"""
return
[docs] def serve_forever(self, stop_timeout=None):
"""Start the service if it hasn't been already started and wait until it's stopped."""
if not self.started:
self.start()
try:
self._stopped_event.wait()
except:
self.stop(timeout=stop_timeout)
raise