def DoTestInterpInThread(cookie):
try:
pythoncom.CoInitialize()
myThread = win32api.GetCurrentThreadId()
GIT = CreateGIT()
interp = GIT.GetInterfaceFromGlobal(cookie, pythoncom.IID_IDispatch)
interp = win32com.client.Dispatch(interp)
TestInterp(interp)
interp.Exec("import win32api")
print "The test thread id is %d, Python.Interpreter's thread ID is %d" % (myThread, interp.Eval("win32api.GetCurrentThreadId()"))
interp = None
pythoncom.CoUninitialize()
except:
traceback.print_exc()
python类CoUninitialize()的实例源码
def DoTestInterpInThread(cookie):
try:
pythoncom.CoInitialize()
myThread = win32api.GetCurrentThreadId()
GIT = CreateGIT()
interp = GIT.GetInterfaceFromGlobal(cookie, pythoncom.IID_IDispatch)
interp = win32com.client.Dispatch(interp)
TestInterp(interp)
interp.Exec("import win32api")
print "The test thread id is %d, Python.Interpreter's thread ID is %d" % (myThread, interp.Eval("win32api.GetCurrentThreadId()"))
interp = None
pythoncom.CoUninitialize()
except:
traceback.print_exc()
def DoTestInterpInThread(cookie):
try:
pythoncom.CoInitialize()
myThread = win32api.GetCurrentThreadId()
GIT = CreateGIT()
interp = GIT.GetInterfaceFromGlobal(cookie, pythoncom.IID_IDispatch)
interp = win32com.client.Dispatch(interp)
TestInterp(interp)
interp.Exec("import win32api")
print "The test thread id is %d, Python.Interpreter's thread ID is %d" % (myThread, interp.Eval("win32api.GetCurrentThreadId()"))
interp = None
pythoncom.CoUninitialize()
except:
traceback.print_exc()
def DoTestInterpInThread(cookie):
try:
pythoncom.CoInitialize()
myThread = win32api.GetCurrentThreadId()
GIT = CreateGIT()
interp = GIT.GetInterfaceFromGlobal(cookie, pythoncom.IID_IDispatch)
interp = win32com.client.Dispatch(interp)
TestInterp(interp)
interp.Exec("import win32api")
print("The test thread id is %d, Python.Interpreter's thread ID is %d" % (myThread, interp.Eval("win32api.GetCurrentThreadId()")))
interp = None
pythoncom.CoUninitialize()
except:
traceback.print_exc()
def stop(self):
"""Stop the running thread gracefully."""
print("Stopping thread...")
self.keep_running = False
# Wait for the thread to stop
self.wait()
print("Thread stopped")
# Uninitialize at thread stop (used for WMI in thread)
pythoncom.CoUninitialize()
def serve(clsids):
infos = factory.RegisterClassFactories(clsids)
pythoncom.EnableQuitMessage(win32api.GetCurrentThreadId())
pythoncom.CoResumeClassObjects()
pythoncom.PumpMessages()
factory.RevokeClassFactories( infos )
pythoncom.CoUninitialize()
def _doTestInThread(self, interp):
pythoncom.CoInitialize()
myThread = win32api.GetCurrentThreadId()
if freeThreaded:
interp = pythoncom.CoGetInterfaceAndReleaseStream(interp, pythoncom.IID_IDispatch)
interp = win32com.client.Dispatch(interp)
interp.Exec("import win32api")
#print "The test thread id is %d, Python.Interpreter's thread ID is %d" % (myThread, interp.Eval("win32api.GetCurrentThreadId()"))
pythoncom.CoUninitialize()
def stop(self):
"""Stop the running thread gracefully."""
print("Stopping thread...")
self.keep_running = False
# Wait for the thread to stop
self.wait()
print("Thread stopped")
# Uninitialize at thread stop (used for WMI in thread)
if os.name == 'nt':
import pythoncom
pythoncom.CoUninitialize()
def serve(clsids):
infos = factory.RegisterClassFactories(clsids)
pythoncom.EnableQuitMessage(win32api.GetCurrentThreadId())
pythoncom.CoResumeClassObjects()
pythoncom.PumpMessages()
factory.RevokeClassFactories( infos )
pythoncom.CoUninitialize()
def _doTestInThread(self, interp):
pythoncom.CoInitialize()
myThread = win32api.GetCurrentThreadId()
if freeThreaded:
interp = pythoncom.CoGetInterfaceAndReleaseStream(interp, pythoncom.IID_IDispatch)
interp = win32com.client.Dispatch(interp)
interp.Exec("import win32api")
#print "The test thread id is %d, Python.Interpreter's thread ID is %d" % (myThread, interp.Eval("win32api.GetCurrentThreadId()"))
pythoncom.CoUninitialize()
def deinitPerThread(self):
import pythoncom
pythoncom.CoUninitialize()
def deinit(self):
import pythoncom
from win32file import CloseHandle
for h in self.handles:
if h is not None:
CloseHandle(h)
self.handles = None
pythoncom.CoUninitialize()
pass
def testall():
dotestall()
pythoncom.CoUninitialize()
print "AXScript Host worked correctly - %d/%d COM objects left alive." % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
def serve(clsids):
infos = factory.RegisterClassFactories(clsids)
pythoncom.EnableQuitMessage(win32api.GetCurrentThreadId())
pythoncom.CoResumeClassObjects()
pythoncom.PumpMessages()
factory.RevokeClassFactories( infos )
pythoncom.CoUninitialize()
def _doTestInThread(self, interp):
pythoncom.CoInitialize()
myThread = win32api.GetCurrentThreadId()
if freeThreaded:
interp = pythoncom.CoGetInterfaceAndReleaseStream(interp, pythoncom.IID_IDispatch)
interp = win32com.client.Dispatch(interp)
interp.Exec("import win32api")
#print "The test thread id is %d, Python.Interpreter's thread ID is %d" % (myThread, interp.Eval("win32api.GetCurrentThreadId()"))
pythoncom.CoUninitialize()
def testall():
dotestall()
pythoncom.CoUninitialize()
print("AXScript Host worked correctly - %d/%d COM objects left alive." % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount()))
def serve(clsids):
infos = factory.RegisterClassFactories(clsids)
pythoncom.EnableQuitMessage(win32api.GetCurrentThreadId())
pythoncom.CoResumeClassObjects()
pythoncom.PumpMessages()
factory.RevokeClassFactories( infos )
pythoncom.CoUninitialize()
def _doTestInThread(self, interp):
pythoncom.CoInitialize()
myThread = win32api.GetCurrentThreadId()
if freeThreaded:
interp = pythoncom.CoGetInterfaceAndReleaseStream(interp, pythoncom.IID_IDispatch)
interp = win32com.client.Dispatch(interp)
interp.Exec("import win32api")
#print "The test thread id is %d, Python.Interpreter's thread ID is %d" % (myThread, interp.Eval("win32api.GetCurrentThreadId()"))
pythoncom.CoUninitialize()
def Quit(self):
self.excel.Quit()
pythoncom.CoUninitialize()
def Quit(self):
self.word.Quit()
#self.word.Quit()
#os.system("taskkill /im WINWORD.exe")
pythoncom.CoUninitialize()
def InstallInWebKit(appServer):
# This function gets called by the app server during initialization.
if not appServer.setting('EnableCOM', False):
return # enabling COM was not requested
# This must be done BEFORE pythoncom is imported -- see the book mentioned above.
import sys
sys.coinit_flags = 0
# Get the win32 extensions
import pythoncom
# Set references to the COM initialize and uninitialize functions
appServer._initCOM = pythoncom.COINIT_MULTITHREADED
appServer.initCOM = pythoncom.CoInitializeEx
appServer.closeCOM = pythoncom.CoUninitialize
# Monkey-patch this instance of the appServer
# Grab references to the original initThread and delThread bound
# methods, which we will replace
appServer.originalInitThread = appServer.initThread
appServer.originalDelThread = appServer.delThread
# Create new versions of initThread and delThread which will call the
# old versions
def newInitThread(self):
# This must be called at the beginning of any thread that uses COM
self.initCOM(self._initCOM)
# Call the original initThread
self.originalInitThread()
def newDelThread(self):
# Call the original delThread
self.originalDelThread()
# Uninitialize COM
self.closeCOM()
# Replace the initThread and delThread methods with our new versions
import new
appServer.initThread = new.instancemethod(newInitThread, appServer, appServer.__class__)
appServer.delThread = new.instancemethod(newDelThread, appServer, appServer.__class__)
print 'COM has been enabled.'