Python C API

  • Thread starter googler.1.webmaster
  • Start date
G

googler.1.webmaster

Hi :)

I have a main() function of my app which intializes the Python
Interpreter and some other stuff. When I am finished I call:

PyGILState state = PyGILState_Ensure()
//call PyRun_String()
PyGILStateRelease(state);

The first question is, I found out the API contains other commands lik
PyEval_AcquireLock(). I don't really understand if I have to use them
too, could anyone explain? Thanks.

Okay, back to topic. In PyRun_String() I call a wrapped function. This
wrapped function does some internal calculations and takes a pointer
to another function which is called in another thread while it
calculates the stuff.

void MyProgressbar(Real p, void* hook) // this function wil be called
in another thread
{
PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject* func = (PyObject*)hook;
//do some python stuff

PyGILState_Release(gilstate)
}

PyObject *pyMyFunction(PyObject *pSelf, PyObject *args, PyObject
*keywords)
{
static char *kwlist[] = {"hook", NULL};

PyObject *hook=NULL;

if (!PyArg_ParseTupleAndKeywords(args, keywords, "O!", kwlist,
&PyFunction_Type, &hook))
return NULL;

LONG ok = MyFunction(myprogress, hook); //hook is a pointer which
is passed to the threaded function.

Py_RETURN_INT(ok);
}

I want to do the same in Python. I want to pass a reference of a
function to the function which is called from the other thread. But it
stops (not crash) here: PyGILState_STATE gilstate = PyGILState_Ensure
();



What can I do? Thank you very much.
 
S

Stefan Behnel

The first question is, I found out the API contains other commands lik
PyEval_AcquireLock(). I don't really understand if I have to use them
too, could anyone explain? Thanks.

That's unrelated. The GIL is special in that it has its own handling functions.

void MyProgressbar(Real p, void* hook) // this function wil be called
in another thread
{
PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject* func = (PyObject*)hook;
//do some python stuff

PyGILState_Release(gilstate)
}

PyObject *pyMyFunction(PyObject *pSelf, PyObject *args, PyObject
*keywords)
{
static char *kwlist[] = {"hook", NULL};

PyObject *hook=NULL;

if (!PyArg_ParseTupleAndKeywords(args, keywords, "O!", kwlist,
&PyFunction_Type, &hook))
return NULL;

You have to release the GIL around the following function call, so that the
thread above can acuire it while you wait for the function to return in
this thread here (which I assume you do).
LONG ok = MyFunction(myprogress, hook); //hook is a pointer which
is passed to the threaded function.

Stefan
 
G

googler.1.webmaster

Hi!

thats a very interesting point and good to know. I have to release
the GIL but how do I do?
In this case i need PyEval_AcquireLock and PyEval_ReleaseLock?


Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top