When using Python a multi-threaded environment, it is quite easy to deadlock as we need to lock the Python GIL. While trying to lock the GIL, we may hold other locks and the Python thread holding the GIL may call us trying to get the same lock. Ideally, we'd have a PyGILSTATE_TryEnsure() such that on failure we can examine the locks we hold and raise a "would deadlock" exception. I couldn't find anything in the Python docs that allows me to do this.
Is there a way to achieve this?