Skip to main content

a Deferred-aware profiler for Twisted

Project description


A Deferred-aware profiler for Python code.

While cProfile is a very useful utility, it is limited to recording synchronous execution time. A function that returns a Deferred will typically return very quickly, while the Deferred it returns might not fire for seconds or even minutes. This is where theseus comes in: any function that returns a Deferred will be tracked by theseus. The time from when the Deferred was returned to when it fired will be measured, and recorded along with the function’s call stack.


The public interface of theseus is a class called Tracer. To get started:

from theseus import Tracer
t = Tracer()

This is enough to start tracing execution. Eventually, the statistics will have to be written to disk:

with open('callgrind.theseus', 'wb') as outfile:

The output is written in callgrind format, which means that standard tools can be used to interpret the results, such as kcachegrind.

If at any point the Tracer is no longer useful, it can be uninstalled to stop tracing:


Additionally, theseus is aware of inlineCallbacks, and will rewrite call stacks to make them look “correct”. For example, given this code:

from twisted.internet import defer, task

def func(reactor):
  yield task.deferLater(reactor, 1, lambda: None)


The call stack according to theseus will look like this (most recent call last):

__main__ in <module>
twisted.internet.task in react
__main__ in func

While theseus and cProfile both use a profile hook, as long as cProfile is installed first, both profilers can be used at the same time. In this case, calling uninstall() will restore cProfile.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for twisted-theseus, version
Filename, size File type Python version Upload date Hashes
Filename, size twisted_theseus- (9.3 kB) File type Wheel Python version pp27 Upload date Hashes View hashes
Filename, size twisted-theseus- (28.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page