Is there an easy way to tell how much time was spent on the Python GIL?

I have a long-running Python service, and I would like to know how many accumulated wall hours were spent on any running threads (i.e. threads that were not blocked for any other reason), waiting for the GIL. Is there an easy way to do this? For example, perhaps I could periodically upload some counter to my log file.

My main motivation is to eliminate the GIL as a source of delayed covert responses from these lengthy processes. There is no reason to suspect GIL (other than that it will be consistent with the symptoms), but other forms of logging have not yet appeared, so if this is easy, it would be nice to have this information.

+5
source share
2 answers

I do not think this is an easy way. This is probably an inconvenient way to rebuild Python to traverse the PyThreadState list and count the threads every time a lock is obtained, but I doubt it is worth the effort!

I know this is a speculative question, but if you are even somewhat worried that there are delays caused by streaming, it might be wise to switch to a multi-processor model instead of a multi-thread model. Because processes are safer and more scalable in Python, they are almost always the best choice if they are practical.

+3
source

GIL gil_load.

. .

0

All Articles