Python/C++ timer intermittent bug
Thomas Jollans
thomas at jollans.com
Thu Jul 1 13:23:53 EDT 2010
On 06/30/2010 09:28 PM, Paul at mail.python.org wrote:
> I have a problem with threading using the Python/C API. I have an
> extension that implements a timer, and the C++ timer callback function
> calls a Python function. The relevant code looks like this:
>
> [snip]
>
> static void CALLBACK PeriodicTimer(UINT wTimerID, UINT msg,
> DWORD dwUser, DWORD dw1, DWORD dw2)
This looks frightfully WIN32. How are you calling a timer? I'm guessing
you're using some win32 function. So my tentative tip on where the
problem might lie is the interaction of Python's PyThreads and the win32
threading primitives you're probably calling.
> {
> PyGILState_STATE pgs;
>
> pgs = PyGILState_Ensure();
> if(attrsetFlag)
> {
> pres = PyObject_CallFunction(attr,NULL);
> if( pres == NULL )printf("CallFunction failed!\n");
> }
> PyGILState_Release( pgs );
>
> }
>
> The Python code that sets this up looks like this:
>
> fetimer.setmodname("Timeslice3")
> fetimer.setprocname("Timetester")
I'm sure this isn't the problem, but why aren't you just passing in an
object? As in fetimer.setcallable(Timeslice3.Timetester)?
> [ snip ]
>
> Fatal Python Error: This thread state must be current when releasing
>
> Fatal Python Error: PyThreadState_DeleteCurrent: no current tstate
>
> Fatal Python Error: PyEval_SaveThread: NULL tstate
As I said, I'd expect there to be some irregularities between what
PyThreads would normally do and what you're doing in the code you didn't
post.
>
> Can anybody help me make this code stable, so that it works all the
> time?
I can't really help you - I have limited experience with the C API, let
alone Python/C threading, and know next to nothing about Windows
programming. Maybe you should ask in a more specialized (and quieter)
forum, such as the CAPI-SIG mailing list, or python-win32.
-- Thomas
More information about the Python-list
mailing list