Thread termination
Stefan Schukat
SSchukat at dspace.de
Tue Oct 17 04:20:40 EDT 2006
Hello
you are using the module variable ie inside the Generic Function, but
you have to use "d" since this is the Python object
which is allowed to access the COM object in the separate thread.
Stefan
________________________________
From: Tejovathi P [mailto:tejovathi.p at gmail.com]
Sent: Monday, October 16, 2006 9:47 AM
To: Stefan Schukat
Subject: Re: Thread termination
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.application')
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_IDispatc
h,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
On 10/13/06, Stefan Schukat <SSchukat at dspace.de> wrote:
Reading from your last posts you are using COM objects.
Therefore you
should not "kill" the thread since this
would lead to reference leaks. So there are only two
ways left:
1. Program a specific interpreter and not use python.exe
which
implements an access to PyThreadState_SetAsyncExc
2. Check in the separate thread at specific times a
variable which is
set in the main thread.
Both need the try finally construct in the
threadfunction to release COM
objects in the right way.
I.e., (taking the source from Roger):
def ThreadFunction(istream, dest):
pythoncom.CoInitialize() // Initialize COM Runtime
for this thread
try:
d=pythoncom.CoGetInterfaceAndReleaseStream(istream,
pythoncom.IID_IDispatch)
my_ie=win32com.client.Dispatch (d)
my_ie.Navigate(dest)
finally:
my_ie = None // Release COM object
pythoncom.CoUninitialize() // Release COM
Runtime for this
thread
Stefan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061017/94f82a7e/attachment.html>
More information about the Python-list
mailing list