[Swig-user] How to release Python GIL

504 views
Skip to first unread message

Ezequiel Ruiz

unread,
Jan 12, 2011, 4:15:23 PM1/12/11
to swig...@lists.sourceforge.net
Hello everyone,

I'm using SWIG to make a Windows library for Python, written in C++. The
problem is that I don't know how to release the Python's GIL for some
specific methods, is it possible to do that? Do I have to set some flags
in the SWIG script?, or write some custom code in my C++ lib?

Thanks in advance for your help,

Ezequiel


------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand
malware threats, the impact they can have on your business, and how you
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Swig-user mailing list
Swig...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user

Brian Cole

unread,
Jan 12, 2011, 5:25:54 PM1/12/11
to Ezequiel Ruiz, swig...@lists.sourceforge.net
The easiest way is to just specify the " -threads" argument to SWIG. This will release the GIL upon every entry into the your library. 

However, it can be quite onerous to release and acquire the GIL around every entry into C++. For example, simple getters and setters probably shouldn't be protected because the critical section is so short that the added overhead of the GIL may kill performance. We use a combination of turning on threads support for the module and then immediately turning it back off again. 

%module(threads="1") MyModule
%nothread;

Then whenever including a header you know you want to acquire and release the GIL around do the following: 

%thread;
%include "MyExpensiveFunctions.h"
%nothread;

I think you can do it on a per-class/per-function basis as well, but I haven't tested this:

%thread MyClass;

Also, if you're writing explicit typemaps you'll need to know about the SWIG_PYTHON_THREAD_(BEGIN/END)_(ALLOW/BLOCK) macros as well. These correspond to Py_BEGIN_ALLOW_THREADS and Py_BLOCK_THREADS respectively: http://docs.python.org/c-api/init.html#Py_BEGIN_ALLOW_THREADS

-Brian

Matthieu Brucher

unread,
Jan 12, 2011, 6:01:16 PM1/12/11
to Ezequiel Ruiz, swig...@lists.sourceforge.net
Hi,

Shameless ad : http://matt.eifelle.com/2007/11/23/enabling-thread-support-in-swig-and-python/

Cheers,

Matthieu

2011/1/12 Ezequiel Ruiz <er...@barcelona04.com>



--
Information System Engineer, Ph.D.
Blog: http://matt.eifelle.com
LinkedIn: http://www.linkedin.com/in/matthieubrucher
Reply all
Reply to author
Forward
0 new messages