is it possible to list all blocked tornado coroutines
<p>I have a "gateway" app written in <code>tornado</code> using <code>@tornado.gen.coroutine</code> to transfer information from one handler to another. I'm trying to do some debugging/status testing. What I'd like to be able to do is enumerate all of the currently blocked/waiting coroutines that are live at a given moment. Is this information accessible somewhere in tornado?</p>

<p>No there isn't, but you could perhaps create your own decorator that wraps gen.coroutine, then updates a data structure when the coroutine begins.</p>

<pre><code>import weakref
import functools

from tornado import gen
from tornado.ioloop import IOLoop

all_coroutines = weakref.WeakKeyDictionary()


def tracked_coroutine(fn):
    coro = gen.coroutine(fn)

    @functools.wraps(coro)
    def start(*args, **kwargs):
        future = coro(*args, **kwargs)
        all_coroutines[future] = str(fn)
        return future

    return start


@tracked_coroutine
def five_second_coroutine():
    yield gen.sleep(5)


@tracked_coroutine
def ten_second_coroutine():
    yield gen.sleep(10)


@gen.coroutine
def tracker():
    while True:
        running = list(all_coroutines.values()) 
        print(running)
        yield gen.sleep(1)


loop = IOLoop.current()
loop.spawn_callback(tracker)
loop.spawn_callback(five_second_coroutine)
loop.spawn_callback(ten_second_coroutine)
loop.start()
</code></pre>

<p>If you run this script for a few seconds you'll see two active coroutines, then one, then none.</p>

<p>Note the <a href="https://docs.python.org/2.7/library/weakref.html#weakref.WeakKeyDictionary" rel="nofollow">warning in the docs about the dictionary changing size</a>, you should catch "RuntimeError" in "tracker" to deal with that problem.</p>

<p>This is a bit complex, you might get all you need much more simply by turning on Tornado's logging and using <a href="http://www.tornadoweb.org/en/stable/ioloop.html#tornado.ioloop.IOLoop.set_blocking_log_threshold" rel="nofollow">set_blocking_log_threshold</a>.</p>

python tornado