This is how you do it:
In the single-threaded case there is one interpreter and one thread state.
In the generic case the way you enumerate the thread states is:
It should be fine to ignore the multiple interpreter thing, no one really uses that feature nowadays.
Related: it would be great to have a way to generate one flamegraph svg file active thread in a multithreaded python program.
I gave a few more details about this in #41 .
This is probably the next major feature I'll add to Pyflame (after fixing #34, which has a PR by me out). However, I'm going to spend most of November traveling. If someone wants to surprise me with a PR to fix this in the interim that would be great, otherwise I'll probably get some time in December.
Did some initial work on this at batterseapower@36ba9e1
Right now it doesn't seem to be able to find interp_head, which is necessary if we want to do anything interesting (PyThreadState_Current is NULL if the GIL is not held). This should be fixable because e.g. nm -a knows about interp_head.
Fixed that, but PyThreadState.frame seems to always be null in the cases I looked at.