Skip to content

Instantly share code, notes, and snippets.

@dpnova
Created November 9, 2016 19:29
Show Gist options
  • Save dpnova/0f8c3cc9ed6b1097f8812a2d76b87621 to your computer and use it in GitHub Desktop.
Save dpnova/0f8c3cc9ed6b1097f8812a2d76b87621 to your computer and use it in GitHub Desktop.
"""
Dump current deferred state.
> Anyway, given a tree of deferreds, this will produce a whatever.dot
> file, which can be fed to graphviz' "dot -Tpdf whatever.dot >
> whatever.pdf", >and the pdf has a rather nice-looking graph of the
> deferreds.
http://twistedmatrix.com/pipermail/twisted-python/2012-July/025867.html
have a look at this too:
https://www.twistedmatrix.com/pipermail/twisted-python/2008-October/018535.html
"""
import gc
import sys
import twisted.internet.defer
def escape(callback_type, obj):
"""Escape the callback."""
dummy = callback_type # noqa
return '%s' % (
str(obj).replace(' ', '_')
.replace(':', '').replace('<', '').replace('>', ''), )
def dump(outfile=sys.stdout):
"""Dump out data for a moment in time."""
outfile.write('digraph deferred_digraph {\n')
for obj in gc.get_objects():
if isinstance(obj, twisted.internet.defer.Deferred):
len_callbacks = len(obj.callbacks)
if obj.callbacks:
outfile.write(
'\t%s -> %s\n' % (
escape('', obj),
escape('', obj.callbacks[0][0][0])))
outfile.write(
'\t%s -> %s\n' % (
escape('', obj),
escape('', obj.callbacks[0][1][0])))
for callbackpairno in range(len_callbacks - 1):
current_callback = obj.callbacks[callbackpairno][0]
current_errback = obj.callbacks[callbackpairno][1]
current_callback_desc = current_callback[0]
current_errback_desc = current_errback[0]
next_callback = obj.callbacks[callbackpairno + 1][0]
next_errback = obj.callbacks[callbackpairno + 1][1]
next_callback_desc = next_callback[0]
next_errback_desc = next_errback[0]
outfile.write(
'\t%s -> %s;\n' % (
escape('callback', current_callback_desc),
escape('callback', next_callback_desc)))
outfile.write(
'\t%s -> %s;\n' % (
escape('errback', current_errback_desc),
escape('errback', next_errback_desc))
)
outfile.write(
'\t%s -> %s;\n' % (
escape('callback', current_callback_desc),
escape('errback', next_errback_desc))
)
outfile.write(
'\t%s -> %s;\n' % (
escape('errback', current_errback_desc),
escape('callback', next_callback_desc))
)
outfile.write('}\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment