Is Python/C api thread safety enough?

Discussion in 'Python' started by Casper, Nov 20, 2004.

  1. Casper

    Casper Guest

    i make a multi thread C/C++ program emded python, each thread running a python sub-interpreter, to make it safety i make code as following between acquire/release the globle interpreter lock:
    Py_BEGIN_ALLOW_THREADS
    func_take_a_long_time()
    Py_END_ALLOW_THREADS
    according to the python document this will avoid to block other thread that running python code since the function will take a long time, but i am wandered if the code of func_take_a_long_time() call a Python/C api, is this conflict with other threads and cause fatal error? i have release the interpreter lock in Py_BEGIN_ALLOW_THREADS and without protected from thread conflict, so if the func_take_a_long_time() call a Python/C api, it perhaps use other thread's context to running python code.

    Can sombody tell me is this possible? Thanks first.


    Donnie Leen
     
    Casper, Nov 20, 2004
    #1
    1. Advertising

  2. Casper

    Nick Coghlan Guest

    Casper wrote:
    > Can sombody tell me is this possible? Thanks first.


    If you're going to call the Python C/API, you need to be holding the GIL. So you
    either can't release it, or the long-running function has to use
    PyGILState_Ensure when it needs to call the Python C/API.

    Cheers,
    Nick.
     
    Nick Coghlan, Nov 20, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hans

    What is thread safety?

    Hans, Oct 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    602
    Sahil Malik
    Oct 12, 2004
  2. Ames Andreas (MPA/DF)

    weakref and thread safety (in python 2.1)

    Ames Andreas (MPA/DF), Jul 22, 2003, in forum: Python
    Replies:
    1
    Views:
    443
    Duncan Booth
    Jul 22, 2003
  3. Donnie Leen
    Replies:
    2
    Views:
    400
    Greg Ewing
    Nov 25, 2004
  4. Robin Haswell

    DB API and thread safety

    Robin Haswell, Jan 20, 2006, in forum: Python
    Replies:
    2
    Views:
    331
    Daniel Dittmar
    Jan 20, 2006
  5. pek
    Replies:
    2
    Views:
    1,120
Loading...

Share This Page