[Tutor] Embedded Python question

Michael P. Reilly arcege@speakeasy.net
Wed, 13 Jun 2001 18:43:09 -0400 (EDT)


Alan Gauld wrote
> 
> This comes from the Czech translators of my tutor.
> I only use Python for prototyping so have no experience 
> of embedding. However I seem to recall some folks asking 
> here so...
> 
> >an advice regarding running more than 1 Python embedded interpreter at the
> >same time? Or could you please give us any impulse where we can find out it?
> >In the concrete: we need run the Py_RunSimpleString() method
> >from various C++ threads at the same time.
> 
> ie, They want to know can they run multiple interpreters (or 
> more accurately call the function) from multiple threads 
> within a single process? Is Python threadsafe at that level? 
> I have no idea, does anyone else?

If they don't care about different interpreters (with different namespaces
and not sharing variables and modules), then they can just wrap the code
some of the standard macros around the functions:

  Py_BEGIN_BLOCK_THREADS
  PyRun_SimpleString(...);
  Py_BEGIN_UNBLOCK_THREADS

Otherwise you have to start getting into the lower level thread state
functions.

  PyThreadState *tstate_1, *tstate_2;

  Py_Initialize();
  PyEval_InitThreads();
  tstate_1 = Py_NewInterpreter();
  tstate_2 = Py_NewInterpreter();
  /* create threads */
  ...
  /* in thread 1 */
  PyEval_AcquireThread(tstate_1);
  PyRun_SimpleString(...);
  PyEval_ReleaseThread(tstate_1);
  ...
  /* in thread 2 */
  PyEval_AcquireThread(tstate_2);
  PyRun_SimpleString(...);
  PyEval_ReleaseThread(tstate_2);

This is untested, and I might have some of the functions slightly
incorrect, but the logic should be sound.  It could be that the
interpreters are created inside the peer threads instead of the main
thread (it looks safe enough).  Tim Peters could probably give a more
definitive answer tho.

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |