[python-win32] problem accessing OPC clients from thread

Jeff Peery jeffpeery at seametrics.com
Tue Dec 4 02:16:43 CET 2007


Ok, thanks.

 

If I have a multithreaded application but use COM objects only within the
secondary thread is pythoncom.CoInitialize() still required?

 

So if I understand correctly, I could go the route I'm headed, but my code
might be simpler and more reliable if I setup an event handler for the
DataChange event of the group (as Graham pointed out). If that is the case,
where might I go to learn about how to set that up?

 

I appreciate your help!

 

Jeff

 

-----Original Message-----------------------------------

 

Message: 4

Date: Mon, 03 Dec 2007 14:33:11 -0800

From: Tim Roberts <timr at probo.com>

Subject: Re: [python-win32] problem accessing OPC clients from thread

To: Python-Win32 List <python-win32 at python.org>

Message-ID: <47548427.6000703 at probo.com>

Content-Type: text/plain; charset=UTF-8

 

Jeff Peery wrote:

> 

> Graham,

> 

> thanks for the beta! I think my main problem is that I don?t really

> understand this code; I didn?t write it. Could you recommend a

> book/website that describes what these functions like

> ?group.OPCItems.AddItem? do? For example I don?t really know what a

> ?group? is or what the addItem() parameters are, or how to interpret

> that error code that you interpreted, and I?m not really sure if this

> code will work for other OPC servers other than the automation direct

> server.

> 

 

Although I can understand your frustration, I want to make sure you

understand that the trouble you reported has nothing to do with the OPC

tools or their usage.  It is quite complicated to use COM objects in a

multithreaded application.  A COM object is allowed to declare itself as

single-threaded; in that case, COM takes extra steps to transfer control

over to a single thread before calling that object.

 

To do that, COM has to have some per-thread data structures to

communicate between threads.  The way that setup gets done is by calling

the CoInitialize API (exposed in Python as pythoncom.CoInitialize).  If

a multithreaded application expects to use COM objects in several

threads, it is REQUIRED to call CoInitialize from every thread.  The

pywin32 library will call CoInitialize on the main thread for you, but

YOU are responsible for calling it from your secondary threads.

 

-- 

Tim Roberts, timr at probo.com

Providenza & Boekelheide, Inc.

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20071203/1cdd1627/attachment.htm 


More information about the python-win32 mailing list