473,382 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

COM and Threads

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
11 3108
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

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

"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
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

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

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

"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

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

"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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import...
0
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
34
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; ...
3
by: bygandhi | last post by:
Hi - I am writing a service which will check a process and its threads for their state ( alive or dead ). The process has 5 .net managed threads created using thread.start and each have been...
10
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes,...
6
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
10
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that...
4
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.