[python-win32] PumpMessages in main thread called by embedded Python causes crash

Mark Hammond mhammond at skippinet.com.au
Mon Apr 30 00:48:58 CEST 2012

On 30/04/2012 1:07 AM, reckoner wrote:
> Thanks for your reply.
> Yes, the app works fine if I don't PumpMessages in my own code. There is
> no console that the application fires up when it runs the embedded
> Python code. In this situation, how do I (or, can I?) pump for my own
> messages if the application is doing some other kind of event loop, as
> you describe?

The event loop isn't generally where you handle messages - messages are 
generally handled in a WndProc or similar (ie, by using something like 
win32gui and its message handling support).  You might also want to look 
into hooking messages (pythonwin has some support for that, and another 
package called PyHook might also help).

> Thanks!
> On Saturday, April 28, 2012 10:15:50 PM, Mark Hammond wrote:
>> I don't know why it crashes exactly, but if the other app has its own
>> event loop then things are going to get screwey - PumpMessages never
>> returns (well, not until a WM_QUIT message is received), so their event
>> loop will never run.
>> I'd guess that their event loop and event handlers have some assumptions
>> built in to it - eg, they might be assuming that their event loop as set
>> something up that one of the message handlers relies on.  You running
>> your own event loop will prevent that setup from happening so one of
>> their handlers will crash.
>> Does that app work OK if you don't call PumpMessages?  If so, a work
>> around might be to check if you have a console, and if you don't, assume
>> some other event loop must be running.
>> Mark
>> On 29/04/2012 2:21 AM, reckoner wrote:
>>> I have written some code that catches windows events that also includes
>>> the usual PumpMessages, and all of this works fine when I run it from
>>> the Python interpreter (Python version 2.6). However, when a separate
>>> application that has Python embedded calls the same code,  the calling
>>> application crashes. I have tracked this down to the PumpMessages call
>>> in my code.
>>> My question is why does this work perfectly well from the interpreter,
>>> but cause a crash when called from the embedded Python running in a
>>> separate application. Note that I don't have access to the separate
>>> application in which the Python call is embedded and when it crashes it
>>> does not provide any kind of traceback.  It took a long time to narrow
>>> it down to the PumpMessages call.
>>> Any advice appreciated. I am really stuck here. By the way, this is on
>>> Windows XP 32-bit.
>>> Thanks!
>>> _______________________________________________
>>> python-win32 mailing list
>>> python-win32 at python.org
>>> http://mail.python.org/mailman/listinfo/python-win32

More information about the python-win32 mailing list