py::print doesn't acquire the GIL ? #5559
              
                Unanswered
              
          
                  
                    
                      HunterBelanger
                    
                  
                
                  asked this question in
                Q&A
              
            Replies: 0 comments
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
-
I have a library which uses spdlog for the output, and to make sure things work properly with Jupyter notebooks, I use custom made sinks which just call
py::printto make sure everything goes through the Python stdout. In modifying this library to work with threading in Python, I noticed that when multiple threads try writing to the log, I get GIL errors, because multiple threads are trying to output at the same time. If I add apy::gill_scoped_acquireright before I callpy::print, this problem does not occur (which makes sense to me).I was just surprised by this behavior though, as based on the GIL documentation, I would have expected
py::printto acquire the GIL automatically. It seems to me thatpy::detail::printshould have agil_scoped_acquireto prevent this issue, as from what I can tell, it will always be accessing a Python object when writing. Is there a reason to not have this here that I am missing ? If this is something that should be there, I am happy to add it myself; I just want to make sure there isn't a reason to not have it, or there isn't a better way to accomplish this.Beta Was this translation helpful? Give feedback.
All reactions