14

For debugging purposes, I would like to iterate over all greenlets and obtain their trace traces -- how can I do that with gevent?

Basically, I would like to do the gevent equivalent of this.

Community
  • 1
  • 1
kdt
  • 27,905
  • 33
  • 92
  • 139

2 Answers2

20

You can use the gc module to iterate through all the objects on the heap and search for greenlets. Greenlets store the stack traces as an attribute gr_frame.

import gc
import traceback
from greenlet import greenlet

for ob in gc.get_objects():
    if not isinstance(ob, greenlet):
        continue
    if not ob:
        continue
    log.error(''.join(traceback.format_stack(ob.gr_frame)))
Stephen Diehl
  • 8,271
  • 5
  • 38
  • 56
-1

Gevent's built-in "print_run_info" function will print the stacks of all greenlets, including the stacks of where they were spawned from:

import gevent
gevent.util.print_run_info()

Documentation: http://www.gevent.org/api/gevent.util.html#gevent.util.print_run_info

Jan
  • 41
  • 5
  • The question specifically says " I would like to do the gevent equivalent of this." – Jan Mar 11 '21 at 15:02