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

Nicolas Duchastel nicolas at otelnet.com
Wed Sep 19 17:13:15 EDT 2001


Thanks for the quick response.... but.....

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 ?

Also, chapter 8.1 does indicated 4 different ways to do locking:
  C_Function_ToCallPythonCode()
  {
	PyThreadState* save = PyEval_SaveThread();	// LOCK
	PyImport_ImportModule( ...); // load another module
	PyRun_SimpleString(...);      // execute a 1 line of Python code
	PyPObject_CallMethod(....); // execute a full Python method
	PyEval_RestoreThread(save);		// UNLOCK
  }
OR
  C_Function_ToCallPythonCode()
  {
	PyEval_AcquireLock();	// LOCK
	PyImport_ImportModule( ...); // load another module
	PyRun_SimpleString(...);      // execute a 1 line of Python code
	PyPObject_CallMethod(....); // execute a full Python method
	PyEval_ReleaseLock();		// UNLOCK
  }
OR
  C_Function_ToCallPythonCode()
  {
	PyEval_AcquireThread(??);	// LOCK
	PyImport_ImportModule( ...); // load another module
	PyRun_SimpleString(...);      // execute a 1 line of Python code
	PyPObject_CallMethod(....); // execute a full Python method
	PyEval_ReleaseThread(??);		// UNLOCK
  }
OR
  C_Function_ToCallPythonCode()
  {
	Py_BEGIN_ALLOW_THREADS
		PyImport_ImportModule( ...); // load another module
		PyRun_SimpleString(...);      // execute a 1 line of Python code
		PyPObject_CallMethod(....); // execute a full Python method
	Py_END_ALLOW_THREADS
  }

So which one should I use ?

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.
If so, what happens to the ThreadState object when the 2nd thread
calls PyEval_SaveThread() ? if it doesn't swap it, it will run with
the 1st thread's ThreadState object. If it does swap it, when this
2nd thread will also get interupted, won't the 1st thread get back
to run and thus it will run with the 2nd thread's object !?
I mean, I must be missing someting !?

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 ?

Thus,.. please RTFP (where P is for Posting rather than Manual)

Thanks,

Nicolas

Ignacio Vazquez-Abrams <ignacio at openservices.net> wrote in message news:<mailman.1000924701.15996.python-list at python.org>...
> On 19 Sep 2001, Nicolas Duchastel wrote:
> 
> > I am trying to use the Python/C API function to get an embeded Interpreter
> > into a multi-threaded C++ application. We are using Pthreads on Solaris 7.
> > My application works fine, but only with 1 thread; and then, when I stop it,
> > it core dumps.
> > I am using Python 2.1.1 built on Solaris 7 with the SUN's C++ 5.2 compiler.
> 
> http://www.python.org/doc/current/api/initialization.html
> http://www.python.org/doc/current/api/threads.html



More information about the Python-list mailing list