Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41bcbe3

Browse files
committed
Commit MvL's doc patch for SF bug #221327. This adds an example of
calling into Python from a C thread.
1 parent fc27375 commit 41bcbe3

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Doc/api/init.tex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,28 @@ \section{Thread State and the Global Interpreter Lock
466466
this must be done by a thread that is created by Python or by the main
467467
thread after Python is initialized).
468468

469+
Assuming you have access to an interpreter object, the typical idiom
470+
for calling into Python from a C thread is
471+
472+
\begin{verbatim}
473+
PyThreadState *tstate;
474+
PyObject *result;
475+
476+
/* interp is your reference to an interpreter object. */
477+
tstate = PyThreadState_New(interp);
478+
PyEval_AcquireThread(tstate);
479+
480+
/* Perform Python actions here. */
481+
result = CallSomeFunction();
482+
/* evaluate result */
483+
484+
/* Release the thread. No Python API allowed beyond this point. */
485+
PyEval_ReleaseThread(tstate);
486+
487+
/* You can either delete the thread state, or save it
488+
until you need it the next time. */
489+
PyThreadState_Delete(tstate);
490+
\end{verbatim}
469491

470492
\begin{ctypedesc}{PyInterpreterState}
471493
This data structure represents the state shared by a number of

0 commit comments

Comments
 (0)