pthreads + callbacks again

Giorgenes Gelatti Gelatti at exatas.unisinos.br
Wed May 15 12:15:45 EDT 2002


I have 2 C modules wich lauchs a thread each one e then access a function of the 
other module.

The code I've coded works fine, but, when I type the ENTER key in the python shell 
it gives the error: "Fatal Python error: PyEval_ReleaseThread: wrong thread state"

Here goes parts of my code:

--------------------------- module A -----------------------
void *PerformLoop(void *name)
{
	PyThreadState* novo;
	
	novo = PyThreadState_New(gbl_interpreter);
	while(1) {
		if(cbFoo) {
		PyObject *param, *item; \
		PyEval_AcquireThread(novo); \
		param = PyTuple_New(1); \
		item = Py_BuildValue("s", S); \
		PyTuple_SetItem(param, 0, item); \
		PRINT(A); \
		PyEval_CallObject(cbFoo, param); \
		Py_DECREF(param); \
		PyEval_ReleaseThread(novo);
		}
	}
	PyThreadState_Delete(novo);
}
/*-------------------------------------------------------------------------------*/
static PyObject *pyt_run(PyObject *self , PyObject *args)
{
	int ret; \
	pthread_t tid; \
   	Py_BEGIN_ALLOW_THREADS \
	savePerform = _save; \
	gbl_interpreter = savePerform->interp; \
	ret = pthread_create(&tid, NULL, PerformLoop, NULL); \
	    Py_END_ALLOW_THREADS

	Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *pyt_foo(PyObject *self , PyObject *args)
{
	char *temp;
	
	if (PyArg_ParseTuple(args, "s", &temp)) {
		PRINT(temp);
	}

	Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *pyt_reg(PyObject *self , PyObject *args)
{
	PyObject *temp;

	if (PyArg_ParseTuple(args, "O:set_callback", &temp)) {
		if (!PyCallable_Check(temp)) {
			PyErr_SetString(PyExc_TypeError, "parameter must be 
callable");
			return NULL;
		}
		Py_XINCREF(temp);         /* Add a reference to new callback */
		cbFoo = temp;
	}
	Py_INCREF(Py_None);
    return Py_None;
}


--------------------- module B --------------
exactaly the same as module A.


in my script I do the following:

import A
import B

A.reg(B.foo)
A.run()


------------------------------

Someone knows what is the problem?
Tks();





More information about the Python-list mailing list