Introduction

gevent-tools is a collection of mini-libraries and utilities built on gevent for building gevent applications. There are currently three modules:

  • service Provides a base class for implementing services, which are a nice way to organize large gevent applications. Services contain greenlets and other services, and provide a good foundation to build the components of any application.
  • cluster A distributed roster manager for clusters for building distributed apps that need to know other hosts available in the cluster.
  • util A couple of utility methods for building network applications in gevent

Using services

class gevent_tools.service.Service[source]

Service base class for creating standalone or composable services

A service is a container for two things: other services and greenlets. It then provides a common interface for starting and stopping them, based on a subset of the gevent.baseserver interface. This way you can include StreamServer or WSGIServer as child services.

Service also lets you catch exceptions in the greenlets started from this service and introduces the concept of ready, letting us block start() until the service is actually ready.

started

This property returns whether this service has been started

ready[source]

This property returns whether this service is ready for business

_ready()[source]

Internal convenience function to proclaim readiness

add_service(service)[source]

Add a child service to this service

The service added will be started when this service starts, before its _start() method is called. It will also be stopped when this service stops, before its _stop() method is called.

remove_service(service)[source]

Remove a child service from this service

_start()[source]

Empty implementation of service start. Implement me!

Return service.READY or starting will block until _ready() is called (or ready_timeout is reached).

_stop()[source]

Empty implementation of service start. Implement me!

start(block_until_ready=True)[source]

Public interface for starting this service and children. By default it blocks until ready.

stop(timeout=None)[source]

Stop this service and child services

If the server uses a pool to spawn the requests, then 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.

serve_forever(stop_timeout=None)[source]

Start the service if it hasn’t been already started and wait until it’s stopped.

spawn(func, *args, **kwargs)[source]

Spawn a greenlet under this service

spawn_later(seconds, func, *args, **kwargs)[source]

Spawn a greenlet in the future under this service

catch(type, handler)[source]

Set an error handler for exceptions of type raised in greenlets

Utils

Utility module

Some useful gevent functions.

gevent_tools.util.connect_and_retry(address, source_address=None, max_retries=None, delay=1.0, max_delay=3600)[source]

Connect and retry based on Twisted’s ReconnectingClientFactory

Wraps gevent.socket.create_connection with a loop for max_retries (if given) using delay and max_delay (and several magic constants) to do a linear backoff with builtin jitter to avoid stampede.

gevent_tools.util.line_protocol(socket_or_file, strip=True)[source]

Generator for looping line-based protocol

Takes a socket or file-from-socket and yields on every line. Disconnecting or connection errors will end the loop.

Contents:

Indices and tables

Table Of Contents

Next topic

Service component

This Page