> I'm extending a Python application that uses several com
> objects and threads.
> The origional programmer set the main thread to multithreaded
> by doing this:
>  * sys.coinit_flags = 0
>  * import pythoncom
> This is nessacary because otherwise a com (MapObjects2 from
> ESRI) can't run in
> a thread, but now I need to use another com object, which is
> running in a
> thread as well.

What do you mean exactly by "running in a thread"?  Do these objects run in
their own thread, or is there only a single thread that uses both objects?

It is critically important that any threads you create follow the COM rules
regarding marshalling of COM objects to other threads.  Not doing that can
cause strange results.  See below for more details.

> This com object, made by one of our clients,
> needs to run in a
> single threading model. For some reason, these two com object
> won't work
> together in one application. MapObjects2 works when the
> coini_flags is set to 0
> but then the other com object isn't working and the other way around.

They should be able to run together in the same process, but possibly not in
the same thread.  That's unusual though - most free-threaded applications
set their threading model to be "both" - meaning that if a single-threaded
apartment tries to use it, it can.

What is the exact error you get?

>  3) Made a some test programs, but I had always the same
> problems, except when
> I made 2 functions for the 2 com objects, and started them
> with beginthreadex
> (like described on page 626 from Mark Hammonds book Python
> programming on
> win32) and then coinitialize in the functions, but
> unfortunately, this is not
> an option in our main program

You should read appendix D (Threads), and specifically the information
regarding COM threading models.  It is very important you undertand COMs
threading model when trying to do these things.

What you may be able to do is spin up a new thread *just* to create the
free-threaded object - then use CoMarshalThreadInterfaceInStream() to
marshal it back to your main (single-threaded apartment) thread, and use it
from there.

> Strange enough, we use 2 other comobjects, adotable for mssql
> and a com object
> for sending smses, and these two are always working.

Almost certainly because these objects are marked as supporting "both"
threading models, so don't care what apartment they are created from.

Mark

_______________________________________________
Python-win32 mailing list
Python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to