Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gevent: debug spinning thread?

Tags:

python

gevent

In my gevent-based program, I've got a thread somewhere which is suck in a loop something like:

while True:
    gevent.sleep(0)

How can I figure out which thread this is? Is it possible to list (and get stack traces for) the running threads?

like image 865
David Wolever Avatar asked Aug 05 '26 14:08

David Wolever


1 Answers

method 1. TimeOut

I use this in my code to keep track greenlets that potentially block. A NodeTaskTimeout is raised when this happens. Just wrap your jobs in a Timeout or provide them with a TimeOut object.

 with Timeout(90, False):
        task_jobs.join()
    
    if task_jobs:
        print 'task jobs killed', task_jobs
        task_jobs.kill()
        if settings.DEBUG:
            raise NodeTaskTimeout

This print's out the task if it hangs/blocks/takes to long. Especially nasty are those jobs that depend on each other and cause a deadlock job1 /thread -> job2/thread2 -> job3/thread3 and job/thread3 only finishes when job1 is done wich will never happen because job2 is not done and job2 is not done because of job3 is not done.. you get the idea ;)

Mehtod 2 Settrace

http://www.rfk.id.au/blog/entry/detect-gevent-blocking-with-greenlet-settrace/

but you also need to put code that you suspect to be "spinning" in a with block.

like image 166
Stephan Avatar answered Aug 08 '26 03:08

Stephan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!