Python C API

googler.1.webmaster at spamgourmet.com googler.1.webmaster at spamgourmet.com
Fri Dec 5 03:26:58 EST 2008


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.



More information about the Python-list mailing list