Emebbed Python & Threads

Martin v. Löwis loewis at informatik.hu-berlin.de
Thu Oct 31 03:55:25 EST 2002


"Kevin Wooten" <kwooten at itracs.com> writes:

> We are embedding python in a multithreaded application. Any thread in
> the application may need to run a script in the interpreter. What would
> be the best, performance wise, way to allow this to happen? 

1. Create a single Python interpreter once, and store that in a local
   variable.

2. Whenever you need to make a call into Python from a thread that
   comes "out of nowhere" (from a Python point of view), allocate a
   thread state. If you have thread-local variables, you may want to
   store this in one, to avoid having to create and release thread
   states each time.

3. Before entering Python, do PyEval_AcquireThread.

4. Call any Python API.

5. Call PyEval_ReleaseThread.

6. Release the thread state, unless you want to keep it in a
   thread-local variable.

Regards,
Martin



More information about the Python-list mailing list