Re: Multi Threading embedded python



Well, depends on what you want to achieve :)

First, I don't think you can call Py_Initialize on many threads. You
have special function to initialise different interpreters on per-thread
basis.

However, the main problem is: do you want to share data across your
threads ? If the answer is 'no' then use one interpreter by thread, this
is, by far, the simplest solution (and the most efficient ?).

But if you *do* need to share data, then you are in big trouble man :)
As far as I tried it, multi-threading and Python don't go along very
well ! At least, not with system-threads. Basically, to run some Python
code, you have to hold the GIL (Global Interpreter Lock) and you cannot
*test* it, you have to try holding it, so be prepare to block your
threads that would want to access Python !

What I would recommend (and that's what I do in my own software) is to
use a single thread in which your Python interpreter is running. Then,
use a message system in C++ to send commands and get them evaluated and
sent back (if needed). By doing so, you'll avoid a lot of problems, and
you won't loose significantly performances (or if you care about that,
then Python is definitly not the language you need) nor parrallelism
(you wouldn't be able to run many Python threads at the same time anyways).

Well, I hope that help,

Pierre

amit a écrit :
> Hello,
>
> I am embedding a python script in a C++ application. The script can be
> called simultaneously from multiple threads.
>
> What is the correct way to implement this situation:
>
> 1) Have unique python interpreter instantiations ( Py_Initialize() ) for
> each thread.
>
> 2) Have one python interpreter, and implement a lock on it so it can't
> be called simultaneously by multiple threads?
>
> Thanks
> Amit
.



Relevant Pages

  • Re: 2.6, 3.0, and truly independent intepreters
    ... simple have the python language and the sip/PyQt modules available to ... All we wanted to do was embed the interpreter and language core as ... unloaded many many times in a host app. ... but lack of multiple load and unload support has been another painful ...
    (comp.lang.python)
  • Re: Python vs. Lisp -- please explain
    ... forever from potential Python users. ... and any change to the interpreter is a change to the program. ... with a Lisp chip. ... to say that Python does not compile to machine code. ...
    (comp.lang.python)
  • Re: 2.6, 3.0, and truly independent intepreters
    ... given all the great things about python. ... the machine's multiple cores to the fullest, ... fresh dynamic lib for each thread-independent interpreter (you can't ... be developed, it would be very attractive for companies to support, ...
    (comp.lang.python)
  • Re: 2.6, 3.0, and truly independent intepreters
    ... resident video) from the child process back to the parent process. ... An example would be python scripts that generate video programatically ... into its interpreter. ... documented otherwise) is that the app can make as many contexts as its ...
    (comp.lang.python)
  • Re: More than you ever wanted to know about objects [was: Is everything a refrence or isnt it]
    ... >Please note that I *am* oversimplifying here, and the nitpickers will ... >that they can pick up the details as they need them, which 98% of Python ... the interpreter goes down the ... >If the interpreter keeps walking up the inheritance tree in this way ...
    (comp.lang.python)