Understanding PyEval_InitThreads

Thomas Heller theller at python.net
Wed Nov 20 05:25:49 EST 2002


martin at v.loewis.de (Martin v. Loewis) writes:

> Thomas Heller <theller at python.net> writes:
> 
> > void call_back_into_python(void)
> > {
> >     PyThreadState *pts;
> >     PyEval_AcquireLock();
> >     pts = PyThreadState_New(g_interp);
> >     if (!pts)
> >         Py_FatalErorr(...);
> >     if (NULL != PyThreadState_Swap(pts)
> >         Py_FatalError(...);
> >     /* now call Python functions */
> >     pts = PyThreadState_Swap(NULL);
> >     if (!pts)
> >         Py_FatalError(...);
> >     PyThreadState_Clear(pts);
> >     PyThreadState_Delete(pts);
> >     PyEval_ReleaseLock();
> > }
> 
> I suggest a simpler strategy:
> 
>    pts = PyThreadState_New(g_interp);
>    if (!pts)
>        Py_FatalErorr(...);
>    PyEval_AcquireThread(pts);
>    /* call Python functions */
>    PyEval_ReleaseThread(pts);
>    PyThreadState_Clear(pts);
>    PyThreadState_Delete(pts);
> 

What's the difference? Only less code?

The docs say that PyEval_AcquireThread() is not available when thread
support is disabled at compile time - but this is probably no problem
on the platforms expected.

Thanks,

Thomas



More information about the Python-list mailing list