Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] - نسخه متنی

David Ascher, Alex Martelli, Anna Ravenscroft

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Recipe 17.5. Using SWIG-Generated Modules in a Multithreaded Environment


Credit: Joe VanAndel, Mark Hammond


Problem



You
want to use SWIG-generated modules in a multithreaded environment;
therefore, the C code in those modules must release the Python global
interpreter lock (see the Introduction to Chapter 9 for more information about the global
interpreter lock).


Solution


Use a typemap for SWIG, written by Mark Hammond,
that was posted on comp.lang.python. It maps Win32
API functions that return BOOL to Python functions
that return None and raise exceptions to diagnose
errors. The wrapped function must set the standard Windows global
LastError if it returns FALSE
(indicating that it has detected an error). The wrapping function
also automatically releases the Python global interpreter lock (GIL)
for the duration of the wrapped function's
execution, to allow free multithreading.

%typedef BOOL BOOLAPI
%typemap(python,except) BOOLAPI {
Py_BEGIN_ALLOW_THREADS
$function
Py_END_ALLOW_THREADS
if (!$source) {
$cleanup
return PyWin_SetAPIError("$name");
}
}


Discussion


To use multiple threads effectively, you must release the Python GIL
from your C-coded extension whenever it's safe to do
so. The simplest way to do this with SWIG is to use an
except directive, as shown in the
recipe's typemap. Within the
typemap, you can then use the normal Python C
API's macros
Py_BEGIN_ALLOW_THREADS and
Py_END_ALLOW_THREADS (around the call to the
wrapped function, indicated by the special SWIG directive
$function) to release the GIL and acquire it
again.

Another interesting effect of this simple typemap
is that it turns the C-oriented error-return convention (returning
FALSE and setting a global error indicator code)
into a highly Pythonic convention (raising an exception).


See Also


SWIG and its typemaps are documented at
http://www.swig.org; Windows API
documentation on LastError is available from the
Microsoft MSDN site at http://msdn.microsoft.com; Chapter 9 for general information on threads and
particularly its Introduction for information on the GIL.


/ 394