Re: Re: Is Python/C api thread safety enough?

Discussion in 'Python' started by Donnie Leen, Nov 21, 2004.

  1. Donnie Leen

    Donnie Leen Guest

    Thanks for help.

    If the long-running function comes from third-party library such as zlib.compress(), we don't know wether the function is safety for python thread, it may cause error, how could we avoid this if we have to use it?



    ==================================

    >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.
    >--
    >http://mail.python.org/mailman/listinfo/python-list
    >



    Donnie Leen
    Donnie Leen, Nov 21, 2004
    #1
    1. Advertising

  2. Donnie Leen

    Nick Coghlan Guest

    Re: Is Python/C api thread safety enough?

    Donnie Leen wrote:
    > Thanks for help.
    >
    > If the long-running function comes from third-party library such as
    > zlib.compress(), we don't know wether the function is safety for python
    > thread, it may cause error, how could we avoid this if we have to use it?


    That's a very case-specific question, so I can't really give you a good answer.

    One option is always to just bite the bullet and hang on to the GIL. That can
    lead to lousy behaviour in a multi-threaded app, though.

    Some Python-aware 3rd party libraries (particularly Python extensions which also
    export C API's) may release the GIL themselves around long-running actions.

    If you're convinced that the function won't call in to the Python API (or will
    acquire the GIL itself before doing so), then just release the GIL around the
    long-running call.

    The best source for info on which is the best option would generally be sites
    specific to the library you are using.

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

  3. Donnie Leen

    Greg Ewing Guest

    Re: Is Python/C api thread safety enough?

    Donnie Leen wrote:

    > If the long-running function comes from third-party library
    > such as zlib.compress(), we don't know wether the function is
    > safety for python thread, it may cause error, how could we
    > avoid this if we have to use it?


    A third-party library that knows nothing about Python
    is unlikely to be making any Python/C API calls, so
    you should be safe in that case.

    If you pass a callback function to the library, and
    your callback makes any Python/C API calls, it will
    need to make sure it holds the GIL around those calls.

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
    Greg Ewing, Nov 25, 2004
    #3
    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:
    583
    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:
    430
    Duncan Booth
    Jul 22, 2003
  3. Casper
    Replies:
    1
    Views:
    340
    Nick Coghlan
    Nov 20, 2004
  4. Robin Haswell

    DB API and thread safety

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

Share This Page