By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

COM and Threads

P: n/a
I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????

Oct 13 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
hg
Teja wrote:
I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????
If COM is not thread safe, then use processes

Oct 13 '06 #2

P: n/a

hg wrote:
Teja wrote:
I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????

If COM is not thread safe, then use processes
Thanks a LOT for your reply...... Can u please tell me how to
processes......

Oct 13 '06 #3

P: n/a

"Teja" <te*********@gmail.comwrote:
>I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????
An actual traceback would help.
At a guess, when using COM in a thread
you need to call pythoncom.CoInitialize and
CoUninitialize yourself.

Roger

Oct 13 '06 #4

P: n/a
hg
Teja wrote:
hg wrote:
>Teja wrote:
>>I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????
If COM is not thread safe, then use processes

Thanks a LOT for your reply...... Can u please tell me how to
processes......
I gather pywin32 gives you the trick to do it (CreateProcess ?) as I
gather "fork"ing is not available under Windows.

Oct 13 '06 #5

P: n/a

Roger Upole wrote:
"Teja" <te*********@gmail.comwrote:
I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????

An actual traceback would help.
At a guess, when using COM in a thread
you need to call pythoncom.CoInitialize and
CoUninitialize yourself.

Roger
Actually Roger, this is the scenario....

I create a COM object at the beginnning of the main thread. In the sub
thread, I need to access the same instance of the COM object. If it
were a normal object ie. not a COM obj, i was able to do it. But if it
were a COM object, its giving an attribute error? Should I pass a COM
object to the thread. If so How? Please let me know ASAP... Thnks

Oct 13 '06 #6

P: n/a

hg wrote:
Teja wrote:
hg wrote:
Teja wrote:
I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????

If COM is not thread safe, then use processes
Thanks a LOT for your reply...... Can u please tell me how to
processes......

I gather pywin32 gives you the trick to do it (CreateProcess ?) as I
gather "fork"ing is not available under Windows.
Hg, This is the scenario..

I create a COM object in main. I need to access that object in a thread
or subprocess or whatever. Can u pls tell me how to do it. Its a COM
object.

Thnks

Oct 13 '06 #7

P: n/a

"Teja" <te*********@gmail.comwrote:
>
Roger Upole wrote:
>"Teja" <te*********@gmail.comwrote:
>I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????

An actual traceback would help.
At a guess, when using COM in a thread
you need to call pythoncom.CoInitialize and
CoUninitialize yourself.

Roger

Actually Roger, this is the scenario....

I create a COM object at the beginnning of the main thread. In the sub
thread, I need to access the same instance of the COM object. If it
were a normal object ie. not a COM obj, i was able to do it. But if it
were a COM object, its giving an attribute error? Should I pass a COM
object to the thread. If so How? Please let me know ASAP... Thnks
To pass COM objects between threads, usually they'll need to be marshaled
using pythoncom.CoMarshalInterThreadInterfaceInStream, and unmarshaled
with pythoncom.CoGetInterfaceAndReleaseStream.

Roger

Oct 13 '06 #8

P: n/a

Roger Upole wrote:
"Teja" <te*********@gmail.comwrote:

Roger Upole wrote:
"Teja" <te*********@gmail.comwrote:
I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????


An actual traceback would help.
At a guess, when using COM in a thread
you need to call pythoncom.CoInitialize and
CoUninitialize yourself.

Roger
Actually Roger, this is the scenario....

I create a COM object at the beginnning of the main thread. In the sub
thread, I need to access the same instance of the COM object. If it
were a normal object ie. not a COM obj, i was able to do it. But if it
were a COM object, its giving an attribute error? Should I pass a COM
object to the thread. If so How? Please let me know ASAP... Thnks

To pass COM objects between threads, usually they'll need to be marshaled
using pythoncom.CoMarshalInterThreadInterfaceInStream, and unmarshaled
with pythoncom.CoGetInterfaceAndReleaseStream.

Roger
I really appreciate your quick reply....Can u please let me know how to
do marshalling and unmarshalling or any good refrences to do it.
Because i tried to do it. I got some errors ans so I left it...

Thnks again...

Oct 13 '06 #9

P: n/a

"Teja" <te*********@gmail.comwrote:
>
Roger Upole wrote:
>"Teja" <te*********@gmail.comwrote:
>
Roger Upole wrote:

"Teja" <te*********@gmail.comwrote:
I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????
An actual traceback would help.
At a guess, when using COM in a thread
you need to call pythoncom.CoInitialize and
CoUninitialize yourself.

Roger

Actually Roger, this is the scenario....

I create a COM object at the beginnning of the main thread. In the sub
thread, I need to access the same instance of the COM object. If it
were a normal object ie. not a COM obj, i was able to do it. But if it
were a COM object, its giving an attribute error? Should I pass a COM
object to the thread. If so How? Please let me know ASAP... Thnks

To pass COM objects between threads, usually they'll need to be marshaled
using pythoncom.CoMarshalInterThreadInterfaceInStream, and unmarshaled
with pythoncom.CoGetInterfaceAndReleaseStream.

Roger

I really appreciate your quick reply....Can u please let me know how to
do marshalling and unmarshalling or any good refrences to do it.
Because i tried to do it. I got some errors ans so I left it...

Thnks again...
Here's a simple example using Internet Explorer.

import win32com.client, pythoncom, thread
ie=win32com.client.Dispatch('internetexplorer.appl ication')
ie.Visible=1

def nav(istream, dest):
pythoncom.CoInitialize()
d=pythoncom.CoGetInterfaceAndReleaseStream(istream , pythoncom.IID_IDispatch)
my_ie=win32com.client.Dispatch(d)
my_ie.Navigate(dest)
pythoncom.CoUninitialize()

s=pythoncom.CoMarshalInterThreadInterfaceInStream( pythoncom.IID_IDispatch,ie)
thread.start_new_thread(nav, (s, 'www.google.com'))

Roger
Oct 13 '06 #10

P: n/a

Roger Upole wrote:
"Teja" <te*********@gmail.comwrote:

Roger Upole wrote:
"Teja" <te*********@gmail.comwrote:

Roger Upole wrote:

"Teja" <te*********@gmail.comwrote:
I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????


An actual traceback would help.
At a guess, when using COM in a thread
you need to call pythoncom.CoInitialize and
CoUninitialize yourself.

Roger

Actually Roger, this is the scenario....

I create a COM object at the beginnning of the main thread. In the sub
thread, I need to access the same instance of the COM object. If it
were a normal object ie. not a COM obj, i was able to do it. But if it
were a COM object, its giving an attribute error? Should I pass a COM
object to the thread. If so How? Please let me know ASAP... Thnks


To pass COM objects between threads, usually they'll need to be marshaled
using pythoncom.CoMarshalInterThreadInterfaceInStream, and unmarshaled
with pythoncom.CoGetInterfaceAndReleaseStream.

Roger
I really appreciate your quick reply....Can u please let me know how to
do marshalling and unmarshalling or any good refrences to do it.
Because i tried to do it. I got some errors ans so I left it...

Thnks again...

Here's a simple example using Internet Explorer.

import win32com.client, pythoncom, thread
ie=win32com.client.Dispatch('internetexplorer.appl ication')
ie.Visible=1

def nav(istream, dest):
pythoncom.CoInitialize()
d=pythoncom.CoGetInterfaceAndReleaseStream(istream , pythoncom.IID_IDispatch)
my_ie=win32com.client.Dispatch(d)
my_ie.Navigate(dest)
pythoncom.CoUninitialize()

s=pythoncom.CoMarshalInterThreadInterfaceInStream( pythoncom.IID_IDispatch,ie)
thread.start_new_thread(nav, (s, 'www.google.com'))

Roger
Thnks a lot Roger, Its working gr8..Now, once the thread is started
with start_new_thread, is there any way to terminate it upon user's
request. I have explored and found out that there is no thread.kill().
So wht to do now?

Teja

Oct 13 '06 #11

P: n/a

Teja wrote:
Roger Upole wrote:
"Teja" <te*********@gmail.comwrote:
>
Roger Upole wrote:
>
>"Teja" <te*********@gmail.comwrote:
>
Roger Upole wrote:
>
>"Teja" <te*********@gmail.comwrote:
>I have an application which uses COM 's Dispatch to create a COM based
object. Now I need to upgrade the application to a threaded one. But
its giving an error that COM and threads wont go together. Specifically
its an attribute error at the point where COM object is invoked. Any
pointers please??????
>
>>
>An actual traceback would help.
>At a guess, when using COM in a thread
>you need to call pythoncom.CoInitialize and
>CoUninitialize yourself.
>>
> Roger
>
Actually Roger, this is the scenario....
>
I create a COM object at the beginnning of the main thread. In the sub
thread, I need to access the same instance of the COM object. If it
were a normal object ie. not a COM obj, i was able to do it. But if it
were a COM object, its giving an attribute error? Should I pass a COM
object to the thread. If so How? Please let me know ASAP... Thnks
>
>>
>To pass COM objects between threads, usually they'll need to be marshaled
>using pythoncom.CoMarshalInterThreadInterfaceInStream, and unmarshaled
>with pythoncom.CoGetInterfaceAndReleaseStream.
>>
> Roger
>
I really appreciate your quick reply....Can u please let me know how to
do marshalling and unmarshalling or any good refrences to do it.
Because i tried to do it. I got some errors ans so I left it...
>
Thnks again...
>
Here's a simple example using Internet Explorer.

import win32com.client, pythoncom, thread
ie=win32com.client.Dispatch('internetexplorer.appl ication')
ie.Visible=1

def nav(istream, dest):
pythoncom.CoInitialize()
d=pythoncom.CoGetInterfaceAndReleaseStream(istream , pythoncom.IID_IDispatch)
my_ie=win32com.client.Dispatch(d)
my_ie.Navigate(dest)
pythoncom.CoUninitialize()

s=pythoncom.CoMarshalInterThreadInterfaceInStream( pythoncom.IID_IDispatch,ie)
thread.start_new_thread(nav, (s, 'www.google.com'))

Roger

Thnks a lot Roger, Its working gr8..Now, once the thread is started
with start_new_thread, is there any way to terminate it upon user's
request. I have explored and found out that there is no thread.kill().
So wht to do now?

Teja
HI all,

I have a problem in accesing COM objects in threads. To be precise,
lets assume that I have a class GenericFunctions which is defined as
follows:

import win32com.client, pythoncom, thread
ie=win32com.client.Dispatch('internetexplorer.appl ication')
ie.Visible=1

class GenericFunctions:

def __init__(self):
print "In Constructor of Generic Functions"

def MyNavigate(self,dest):
ie.Navigate(dest)
Now there is another file Main.py which is defined as follows:

import win32com.client, pythoncom, thread
from GenericFunctions import *
obj = GenericFunctions()

class Mainclass:
def __init__(self);
print "In Constructor of Main class"

def threadFunction(self,dest):
pythoncom.CoInitialize()
d=pythoncom.CoGetInterfaceAndReleaseStream(s,
pythoncom.IID_IDispatch)
my_ie=win32com.client.Dispatch(d)
obj.func(dest) # this is gving an error.
pythoncom.CoUninitialize()

if __name__ == "__main__":

s=pythoncom.CoMarshalInterThreadInterfaceInStream( pythoncom.IID_IDispatch,ie)
thread.start_new_thread(self.nav, (s,'www.google.com')

Basically, I want to access object of GenericFunctions class inside
threadFunction(). However I was able to execute
my_ie.Navigate("google.com"). But that was not I wanted. I am not
knowing where the error is....
Please let me know the solution ASAP...

Teja.P

Oct 16 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.