[Cython] [cython-users] What's up with PyEval_InitThreads() in python 2.7?

Stefan Behnel stefan_ml at behnel.de
Tue Feb 28 11:25:54 CET 2012


mark florisson, 28.02.2012 11:16:
> On 28 February 2012 09:54, Stefan Behnel wrote:
>> I'm going to reimplement this, but not for 0.16 anymore, I'd say.
> 
> That's ok, I fixed it to not acquire the GIL seeing that control flow
> obsoletes None initialization. So you might as well move it into the
> setup function if you care, the thing is that that would needlessly
> acquire the GIL for the common case (when you have the GIL) in the
> tests, so it might slow them down. It would be better to create a
> __Pyx_RefNannySetupContextNogil() function wrapper. If you're not
> running the code as a test the preprocessor would filter out this
> bloat though, so it's really not a very big deal anyway.

I was going to pass a constant flag into the macro that would let the C
compiler do the right thing:

"""
#ifdef WITH_THREAD
  #define __Pyx_RefNannySetupContext(name, acquire_gil) \
          if (acquire_gil) { \
              PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure(); \
              __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), ...) \
              PyGILState_Release(__pyx_gilstate_save); \
          } else { \
              __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), ...) \
          }
#else
  #define __Pyx_RefNannySetupContext(name, acquire_gil) \
          __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), ...)
#endif
"""

That also gets rid of the need to declare the "save" variable independently.

Stefan


More information about the cython-devel mailing list