Embeded Multi-Threaded Python: PyEval_InitThread(), PyEval_SaveThread(),...

Ignacio Vazquez-Abrams ignacio at openservices.net
Wed Sep 19 17:37:30 EDT 2001


On 19 Sep 2001, Nicolas Duchastel wrote:

> I actually RTFM-ed it; that's why my question is so detailed.
> The doc seem to be conflicting in some areas and lacks some important
> information.
>
> For example, Chapter 8 states that Py_Initialized() can be called
> before or after PyEval_InitThreads(); so which is best ? what are the
> implications ? any nuance between these two ways ? any examples ?

It doesn't seem as though it matters, just so long as you haven't created any
threads before calling PyEval_InitThreads().

> Also, chapter 8.1 does indicated 4 different ways to do locking:
>
>  [snip]
>
> So which one should I use ?

Probably the one that is clearest, i.e., Py_(BEGIN|END)_ALLOW_THREADS.

> Also, image my 2 distinct thread B and C executing the 1st snipplet of
> code above, if the call to PyObject_CallMethod() is VERY long and
> takes ages,...
> chapter 8 says
>
>    "In order to support multi-threaded Python programs, the
>     interpreter regularly releases and reacquires the lock --
>     by default, every ten bytecode instructions."
>
> Does that thus mean that a 2nd thread running this 1st snipplet
> above will be interupted in the middle of its work in
> PyObject_CallMethod() and another thread will have a go at it.

AFAIK, when you're running a C program that doesn't use PyRun_*() you aren't
actually using any bytecode so there's no reason for there to be a problem.

> Also, what about the things I was asking with regards to the actual OS
> thread's ID ? i.e. does it matter that my code starts in one thread
> and then works from another thread ? do I need to create ThreadState
> objects for each threads ?

The easiest thing to do is probably just to have your main thread both
initialize and finalize the Python environment.

In my experience the most frequent cause of core dumps is that you have under-
or overestimated the need to increment or decrement the reference count of a
Python object. If you're willing, I can take a look at your code and tell you
if that's the case.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>






More information about the Python-list mailing list