<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
dang formatting... trying again:<br>
<br>
&nbsp;i'm still working on this problem where the following code is called
from a wxPython GUI.&nbsp; i have a test program that contains two buttons.&nbsp;
one button calls the play method of the following class and plays an
mp3 file.&nbsp; the second button is brings up a simple message dialog.&nbsp;&nbsp;
the problem is the when the following play method is called and the
music is playing, you are unable to press the button to display the
message dialog.&nbsp;&nbsp; essentially, the GUI app is frozen until the play of
the mp3 file is finished.&nbsp;&nbsp; i thought the PyEval_SaveThread and
PyEval_RestoreThread would work, but not only does it not work,&nbsp; the
app crashes when you press the button the display the message dialog.&nbsp;
as suggested by simon i looked at the dsptools, but i wasn't able to
get anything working with my app.&nbsp; following is my code with the Thread
functions added .&nbsp; i've tested this with the default _callback as well
as a callback from the calling python script.&nbsp; in both cases the app
just&nbsp; hangs after the music is finished playing. i'm also not able to
press the message dialog button while the play is happening.&nbsp;&nbsp; what i
was trying to accomplish is giving back control to python while the c
code is busy, but obviously this is completely wrong.<br>
<br>
bryan<br>
<br>
<br>
cdef extern from "Python.h":&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; ctypedef struct PyThreadState&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; PyThreadState *&nbsp; PyEval_SaveThread()<br>
&nbsp;&nbsp;&nbsp; void PyEval_RestoreThread(PyThreadState *)<br>
<br>
cdef PyThreadState *_thread<br>
<br>
default_callback = eval('lambda *args: 1')<br>
<br>
cdef class Play: <br>
&nbsp;&nbsp;&nbsp; def play(self, callback=None):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; global _thread<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if not callback:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; callback = default_callback&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.callback = callback&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mpgedit_play_set_status_callback(self.ctx, _callback_wrapper,
&lt;void*&gt;callback)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; more = True<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _thread = NULL<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(more): <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _thread = PyEval_SaveThread()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; more = mpgedit_play_frame(self.ctx)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if _thread:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyEval_RestoreThread(_thread)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _thread = NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
<br>
cdef int _callback_wrapper(void *callback, long sec, long msec) except
*:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; global _thread<br>
&nbsp;&nbsp;&nbsp; PyEval_RestoreThread(_thread)<br>
&nbsp;&nbsp;&nbsp; _thread = NULL<br>
&nbsp;&nbsp;&nbsp; return (&lt;object&gt;callback)(sec, msec)<br>
&nbsp;&nbsp;&nbsp; <br>
</body>
</html>