Skip to main content

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)

HOWTO

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

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, myservice.py:

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

MyService.initFromCLI()

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

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

Tasks

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
TornadoHTTPTask.register()
VService.initFromCLI()

Now running it emits:

> python myservice.py --http-port 8000
INFO:VService.TornadoHTTPTask:TornadoHTTPTask Server Started on 0.0.0.0 (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 webserver:

> 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()
PrintClock.register()

from sparts.vservice import VService
VService.initFromCLI()

And the result:

DEBUG:VService:All tasks started
DEBUG:VService:VService Active.  Awaiting graceful shutdown.
1376081805.08
1376081806.08
1376081807.08
1376081808.08
1376081809.08
1376081810.08
1376081811.08

HALP

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

License

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

Metadata

Release files for sparts 0.7.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sparts 0.7.3
File Size Uploaded
sparts-0.7.3.tar.gz 52.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for sparts 0.7.3
File Interpreter ABI Platform
sparts-0.7.3-py3.4.egg Legacy Egg format - - Details
sparts-0.7.3-py2.7.egg Legacy Egg format - - Details

Total release size: 750.7 kB

Release files / sparts-0.7.3.tar.gz

Download URL sparts-0.7.3.tar.gz
Size 52.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b25707c349079a7c888155e7fdfd8394df2da9aca9c8eee4e205528cb16b7662
BLAKE2b-256 checksum
How to use checksums
1c128384777d491f49be54a7d1454b10b35d9dcc5acbe093ae3139b0ac5fccf3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sparts-0.7.3-py3.4.egg

Download URL sparts-0.7.3-py3.4.egg
Size 191.7 kB
Tags Egg
SHA-256 checksum
How to use checksums
ab1d72bca5446474dae395087e82755a21966ae5b4e8b725a4b2b00599117451
BLAKE2b-256 checksum
How to use checksums
a7dad1afce6bc692b6422ba7dac31a78eecbad19dfdcdde9ea31bfe967a3dc08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sparts-0.7.3-py2.7.egg

Download URL sparts-0.7.3-py2.7.egg
Size 506.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
67181609ed396de059106103555cc4a85c349bb43807fe3d247270c335da91c4
BLAKE2b-256 checksum
How to use checksums
8e9f998cd770cc89e8bd313826507c7812d1059570009a0b1112ae66760353ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.7.3 This release

3 release files

0.7.2

2 release files

0.7.1

5 release files

0.7.0

5 release files

0.6.1

5 release files

0.6.0

5 release files

0.5.1

2 release files

0.5.0

4 release files

0.4.1

2 release files

0.4.0

0.3.1

0.2.2

2 release files

0.2.1

0.2.0

0.1.3

0.1.2

0.1.1

0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page