Is Python/C api thread safety enough?

Nick Coghlan ncoghlan at email.com
Mon Nov 22 09:08:19 EST 2004


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.



More information about the Python-list mailing list