Embedding Python in a C extension

MRAB python at mrabarnett.plus.com
Wed Jun 2 20:07:39 EDT 2010


Paul at mail.python.org wrote:
> I have a problem with embedding Python into a C extension in Windows
> Vista. I have implemented a timer routine in C as an extension, which
> I can import into Python 2.6.5 and run. Each timer interval, the
> extension calls a C CALLBACK function. I want to be able to have this
> CALLBACK function call a Python function, so that I can write the
> timer handler in Python.
> 
> I can write Python functions and call them from a C extension function
> with no trouble in all cases EXCEPT when the function that calls the
> Python code is the timer's CALLBACK function. That is, if I call
> PyRun_SimpleString or PyRun_SimpleFile, or PyImport_ImportModule, or
> basically any Python-y function in the CALLBACK function, Python
> crashes and I get a window saying "Python has stopped working. Windows
> is searching for a solution to the problem."
> 
> The Python code runs fine if I call it from a function in the
> extension that is not the CALLBACK function. And regular C code runs
> in the CALLBACK function properly. It only crashes if I try to run the
> Python code from the timer CALLBACK function (or any C function I call
> from the CALLBACK function).
> 
> Does anybody know how to fix this? Is there a workaround of some sort?

Here's a quote from the Python docs at:

     http://docs.python.org/c-api/init.html

"""The Python interpreter is not fully thread safe. In order to support
multi-threaded Python programs, there's a global lock, called the global
interpreter lock or GIL, that must be held by the current thread before
it can safely access Python objects. Without the lock, even the simplest
operations could cause problems in a multi-threaded program: for
example, when two threads simultaneously increment the reference count
of the same object, the reference count could end up being incremented
only once instead of twice."""

That's probably what's happening to you.



More information about the Python-list mailing list