0

I'm trying to wrap a C++ camera frame acquisition library into Python. I'm having problems with the callback function. I call a python function from a C function with the following code:

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
result = PyObject_CallObject(my_callback, arglist);
if(result == NULL){
    caller.stopLive();
}
Py_DECREF(result);
Py_DECREF(arglist);
PyGILState_Release(gstate);

The callback works fine. The problem arises when I try to stop the callback with the camera library functions. If the python callback function is short the camera library function is able to stop the callbacks, but if the python callback function takes longer the camera library function is unable to stop the callbacks and the program freezes.

Has anybody had similar problems? Do you have any suggestions about what to try?

Edit: The code for stopping the callbacks is:

static PyObject * stopcallback(PyObject *self, PyObject *args)
{
    if(grabber->isListenerRegistered(pListener)){
        grabber->removeListener(pListener);
        while(grabber->isListenerRegistered(pListener)){
            Sleep(0);
        }
        delete pListener;
        printf("Callback stopped\n");
        Py_RETURN_NONE;
    }
}

I call this function from Python.

1
  • It isn't clear what you mean by "stop the callbacks"? Can you share the code you used to attempt this? Note that you have the GIL locked here (as you should). Does "stopping the callback" involve running Python code? If so, it will be blocked by the GIL. Commented Jul 17, 2012 at 13:54

1 Answer 1

2

You say "If the python callback function is short the camera library function is able to stop the callbacks".

I wonder whether the library is unable to remove the callback while it is running (or pending?).

If the callback is short, there are periods of time when it's not being run (or is not pending), so the library is able to remove it, but if the callback is long, it's being run (or is pending) virtually all the time, so the library has no opportunity to remove it.

Sign up to request clarification or add additional context in comments.

3 Comments

I think you are correct. The question would then be, how to allow the library to remove the callback. I guess the GIL lock would have to be broken somehow from the C code? Is there a way to do this?
Could you modify the callback function so that it can be short-circuited? For example, starting it with if stopping: return and doing stopping = True when you're about to remove the callback.
I guess I could do it this way, although it seems a bit of a hack. I'll let you know how it goes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.