2

Is there a reliable way to test whether a function holds (doesn't release) the GIL through its entire execution? If so, what is that method?

For example, I want to determine if the key derivation algorithms from hashlib hold the GIL? I thought I could use time.time() to determine whether the GIL was held by checking if the difference between the time after the thread started to before was significantly large. But this option relies upon personal judgment and doesn't account for if both times were collected before the interpreter decided to execute the function in question.

Tankobot
  • 1,492
  • 14
  • 21
  • That would not be possible from inside Python. Any function retrieving this information would have to hold the GIL at that moment. – Klaus D. Dec 27 '16 at 07:15
  • As I understand it thats only relevant if you execute python threads. Every python thread is started as a native os thread on the system. But inside the python interpreter the running thread (only one) "holds" the GIL. Practically the current_thread pointer points at the thread. The GIL is relaesed mainly when the running thread calls an IO function (reading/writing files, sockets etc) then the GIL goes to the next thread, which can start running now ... ans so on. Check this (IMHO) very good overview from david beazley: https://www.youtube.com/watch?v=ph374fJqFPE – klaas Dec 27 '16 at 10:11
  • One more remark. The hashlib module description of the key derivation functiones also gives insication about IO or CPU bounds and even GIL. Quote: "Note A fast implementation of pbkdf2_hmac is available with OpenSSL. The Python implementation uses an inline version of hmac. It is about three times slower and doesn’t release the GIL." See here: https://docs.python.org/3/library/hashlib.html – klaas Dec 27 '16 at 19:43
  • That was one of the reasons I wanted to detect GIL releasing status for a function, to determine if the current implementation being used will release the GIL or hold it. – Tankobot Jan 27 '17 at 09:31
  • Duplicates https://stackoverflow.com/questions/11366556/how-can-i-check-whether-a-thread-currently-holds-the-gil? – lumbric Nov 07 '17 at 09:54
  • @lumbric almost, but this question is asking how to detect if a function ever releases the GIL, as opposed to the linked question which is more concerned with *polling* whether a dead thread currently has the GIL locked. – Tankobot Nov 07 '17 at 10:08

0 Answers0