0

我正在尝试让线程与程序一起工作以查询 Historian 服务器,以便可以执行并发查询。我想要一个可以在程序中传递的服务器实例,而不是在每个工作线程中实例化一个服务器对象。

我遇到的问题是对 CoGetInterfaceAndReleaseStream 的调用导致程序在使用“iHistorian_SDK.Server”时挂起,但在使用其他东西时工作正常,例如“Excel.Application”。这是 Historian SDK 的限制吗?有没有办法让它工作?

import threading
import time

import pythoncom
import win32com.client

def run_in_thread(hist_id):
    # Initialize
    pythoncom.CoInitialize()
    # Get instance from the id
    hist = win32com.client.Dispatch(
        pythoncom.CoGetInterfaceAndReleaseStream(hist_id, pythoncom.IID_IDispatch))
    time.sleep(5)
    pythoncom.CoUninitialize ()


if __name__ == '__main__':
    pythoncom.CoInitialize () # Is this needed in the main thread?
    hist = win32com.client.Dispatch("iHistorian_SDK.Server") #"Excel.Application"
    hist_id = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, hist)

    thread = threading.Thread(target=run_in_thread, kwargs={'hist_id': hist_id})
    thread.start()
    thread.join()
4

0 回答 0