event loop problem

Geoff Talvola gtalvola at nameconnector.com
Mon Oct 23 16:29:02 EDT 2000


I know exactly the problem you're talking about -- and I consider it a huge
design flaw in NT that any one application can cause other applications to
hang if it's not processing its message loop.  Starting up PythonWin is
another example of something you can't do if any program isn't processing
its messages.

There are 2 solutions I can think of.  First of all, win32all already
contains a function that can process all waiting messages:
win32gui.PumpWaitingMessages() inserted into your while loop should do the
trick.

But a better solution is to tell win32com to run in free-threading mode, in
which case it doesn't create a hidden window and everything works fine
without having to process messages.  To do this, add the following lines
BEFORE you import win32com:

    import sys
    sys.coinit_flags = 0

and if you are using multiple threads, each thread besides the main thread
should call

    pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED)

when they are initialized, and

    pythoncom.CoUninitialize()

when they exit.  If you're not using multiple threads, you don't need the
CoInitializeEx and CoUninitialize.

See the excellent book Python Programming on Win32 if you need more
details.

alex116321 at my-deja.com wrote:

> I am trying to write a simple Python script which which uses ADO
> through Python's COM extension but never exit.  For this I have writen
> a simple while loop however this causes a problem on Windows NT.
> Sometimes other windows on NT broadcast messages which need to be
> confirmed by all running windows.  If at least one window does not
> dispatch the confirmation, the broadcaster will hang.  In C++ the
> solution is to make the main event loop listen for windows messages,
> for example:
>
> while(PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE)) {
>   TranslateMessage(&msg);
>   DispatchMessage(&msg);
> }
>
> However, I cannot seem to find an equivalence in Python except to use
> Tkinter and use the mainloop function.  Unfortunately, I do not require
> any graphics in my program.  This is my Python script:
>
> ######################################################
> import time, win32com.client
>
> # Dispatch Microsoft ActiveX Data Objects (ADO)
> rset = win32com.client.Dispatch("ADODB.RecordSet")
>
> query = "select cAFUserAccountP from Person"
> dbstring = "DRIVER={SQL Server};SERVER=svr-tor-
> dev;DATABASE=SMS_HP_1.2.0;UID=sa;PWD="
>
> rset.Open(query, dbstring)
> rset.Close()
>
> while 1:
>    time.sleep(0.01)
>
> ##########################################################
>
> While this script is running, try setting the time through the windows
> Date/Time Properties window.  The window will get stuck until the
> python script is killed.  This is because it tries to broadcast a
> message but the python application window does not respond.  Actually,
> ADO opens a hidden window and causes this problem.  If ADO is not used,
> no problem exists.  Any help would be greatly appreciated.

--


- Geoff Talvola
  Parlance Corporation
  gtalvola at NameConnector.com





More information about the Python-list mailing list