Windows/COM events/strange behavior

Toby Dickenson tdickenson at
Tue Oct 30 12:03:51 CET 2001

"Krzysztof Czarnowski" <Krzysztof.Czarnowski at> wrote:

>Sat, 27 Oct 2001 05:42:26 GMT
>Mark Hammond wrote:
>> As I said, this main thread probably needs to be calling
>> PumpWaitingMessages() each time around the loop.
>Well... This definitely doesn't look very elegant to me!
>Anyway, if I got it well:
>1. I should introduce CoInitialize()/CoUninitialize() pair in the main

The pythoncom module calls CoInitializeEx automatically the first time
it is imported, so you dont need to this this as well.

> *and* call PumpWaitingMessages() in its loop? Right?

Since pythoncom has made your main thread into an apartment threaded
com thread, yes you will need to do this.

> I assume that no
>COM event will be delivered here, or will it?

Only if you subscribe to those events in that thread. 

>2. No need to do this in other child threads (like the CORBA thread). Right?

No (unless you want to do COM stuff in those threads)

>3. Will the problem go away if:
> * the COM event handler is installed in a "free threaded appartment"? (can
>I do this in a threading. thread?)

That wont help, beacuse the main thread is still apartment threaded.

> * native Windows threads from ? (now module threading is used)

No difference.

>And generally, what would be *your* favorite solution? (my task is basically
>to receive events from a COM driven subsystem and pass them to a CORBA

Some other options:

1. Arrange for the first import of pythoncom to happen in a thread
that will actually be using com and providing a message loop, so that
pythoncom's automatic CoInitializeEx call is not wasted. You can do
this either by rearranging your imports, rearranging your threading
structure, or both. This option is either the best, or the craziest,
depending on context.

2. Set sys.coinit_flags *before* importing pythoncom. Pythoncom uses
these flags in its automatic call to CoInitializeEx. If you set
sys.coinit_flags = COINIT_MULTITHREADED, then your main thread will be
free-threaded, and you dont need to provide a message loop.

I hope this helps.

Toby Dickenson
tdickenson at

More information about the Python-list mailing list