I'm a great fan of py-spy for profiling but have been having issues using it with greenlets because greenlets truncate their Python stack at the frame where the greenlet is created so none of the parent frames are available. I've gotten around this with the following patch for greenlet 1.x versions:
diff --git a/src/greenlet/greenlet.c b/src/greenlet/greenlet.c
index f47bbf8..f429dd6 100644
--- a/src/greenlet/greenlet.c
+++ b/src/greenlet/greenlet.c
@@ -873,7 +873,8 @@ static int GREENLET_NOINLINE(g_initialstub)(void* mark)
else {
self->stack_prev = ts_current;
}
- self->top_frame = NULL;
+ /* self->top_frame = NULL; */ /* SG */
+ self->top_frame = PyThreadState_GET()->frame; /* SG */
green_clear_exc(self);
self->recursion_depth = PyThreadState_GET()->recursion_depth;
Presumably something similar for greenlet 2.x but in greenlet_greenlet.hpp in set_initial_state(). I haven't been able to test on 2.x because I've been unable to get gevent to work with it. Any reasons this approach would be a bad idea?