Skip to content
This repository has been archived by the owner. It is now read-only.
Prototype python services with as little code as possible
Branch: master
Clone or download
Latest commit c03df92 Jul 26, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
demo TMultiplexed{Service|Protocol} Proof of Concept Aug 19, 2015
externals Updated to latest fmoo/thrift fork Aug 23, 2014
sparts QueuTask: prevent unhandled exception from tearing the main loop down May 20, 2016
.coveragerc Add branch analysis to .coveragerc Apr 30, 2014
.gitignore Add *.egg to .gitignore Apr 13, 2014
.gitmodules Rename freenoted to sparts. Oct 28, 2012 Add .travis-install to better handle conditional install instructions Aug 1, 2014 Also port pre_install steps to Aug 1, 2014
.travis.yml Officially add 3.5 support Mar 31, 2016
CONTRIBUTING.rst Update CONTRIBUTING.rst Apr 17, 2014
LICENSE Update to standard FB BSD + Patents License Apr 11, 2014 manifest: add manifest Jul 12, 2015
PATENTS Update patent grant: Apr 10, 2015
README.rst fixing minor spelling typos Jul 23, 2017
setup.cfg Add setup.cfg settings for setuptools-pep8 Jun 24, 2014 Officially add 3.5 support Mar 31, 2016


There is a lot of boilerplate and copypasta associated with building services (or any other software that runs continuously and does things)

sparts is a python library developed at Facebook that aims to eliminate as much of the skeleton code as possible, making it as dead simple to write new services with little to no excess code.

Design Goals

  • Be able to implement services with as little code as possible
  • Support as many RPC transports as possible (thrift, HTTP, dbus, etc)
  • Make it painless to integrate services that require custom IO loops (twisted, tornado, glib, etc)


A sparts service typically consists of two parts, the core "service", and its "tasks". Background and offline processing are generally done by tasks, while common or shared functionality belongs to the service.


sparts.vtask.VService - This is the core of any sparts service.

Simply subclass VService for any custom service instance logic, and run its initFromCLI() and you are done.

For example,

from sparts.vservice import VService
class MyService(VService):


Now, you can run this file with the -h option (to see the available options), or run with: python

This should emit something like the following output:

DEBUG:VService:All tasks started``

And pressing ^C will emit:

^CINFO:VService:KeyboardInterrupt Received!  Stopping Tasks...
INFO:VService:Instance shut down gracefully

This simple service, by itself, is pretty damn useless. That's where Tasks come into play


sparts.vtask.VTask - This is the base class for all tasks

Tasks are what trigger your program to take action. This action can be processing periodic events, handling HTTP requests, handling thrift requests, working on items from a queue, waking up on an event, operating some ioloop, or whatever.

Here's a simple example of a service with tasks (requires tornado installed):

from sparts.vservice import VService
from sparts.tasks.tornado import TornadoHTTPTask

Now running it emits:

> python --http-port 8000
INFO:VService.TornadoHTTPTask:TornadoHTTPTask Server Started on (port 8000)
INFO:VService.TornadoHTTPTask:TornadoHTTPTask Server Started on :: (port 8000)
DEBUG:MyService:All tasks started

And as you can see, you can curl the web server:

> curl localhost:8000
Hello, world

Tasks can be subclassed to do all kinds of things. This one prints the current Unix timestamp every second:

from sparts.tasks.periodic import PeriodicTask

class PrintClock(PeriodicTask):
    INTERVAL = 1.0
    def execute(self):
        print time.time()

from sparts.vservice import VService

And the result:

DEBUG:VService:All tasks started
DEBUG:VService:VService Active.  Awaiting graceful shutdown.


If you have any questions, comments, feedback, suggestions, etc, please feel free to contact me at any time.


sparts is BSD-licensed. We also provide an additional patent grant.

You can’t perform that action at this time.