Thread safe tips for python extending

  In fact, the habit of using python write some single-threaded expansion, has not encountered such problems 

  Write a python today in the extension of the time encountered a problem. Through the python launched a c + + thread, the thread c + + will be a pullback python interface. Debugging has been for a long time is not aware of the security thread reasons, because c + + threads in the vm call python when the pyobject. thread is not safe. Therefore, when c + + threading operation vm directly to a certain object it is very wrong result. 

  I search the information in the http://www.python.org/dev/peps/pep-0311/ have talked about how the vm synchronous python. Two vm lock function can be done, and c + + thread synchronization. Is The so-called GIL (Global Interpreter Lock). 
  There are two ways to accomplish the following: 
  1. Py_BEGIN_ALLOW_THREADS, Py_END_ALLOW_THREADS this to Acer 
  2. PyGILState_Ensure and PyGILState_Release 

  But I Py_BEGIN_ALLOW_THREADS not seem useful, I will not have to get to the bottom, because PyGILState_Ensure enough. Do not have too much time to study the documents, to use the time to look back on. 

  I look at the sample code: 

  # Include "Python.h" 
  # Include "ceval.h" 

  Class PyGILHelper 
  ( 
  Public: 
  PyGILHelper (): 
  MState (PyGILState_Ensure ()) 
  ( 
  ) 
  ~ PyGILHelper () 
  ( 
  PyGILState_Release (mState); 
  ) 
  Protected: 
  PyGILState_STATE mState; 
  ); 

  Usage is simple: 

  PyGILHelper lock; 
  / / Some c api code 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply