簡體   English   中英

python,COM和多線程問題

[英]python, COM and multithreading issue

我試圖從調度IE的單獨線程看一下IE的DOM,對於某些屬性我得到“沒有這樣的接口支持”錯誤。 我設法將問題減少到這個腳本:

import threading, time

import pythoncom
from win32com.client import Dispatch, gencache
gencache.EnsureModule('{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}', 0, 4, 0) # MSHTML

def main():
    pythoncom.CoInitializeEx(0)
    ie = Dispatch('InternetExplorer.Application')
    ie.Visible = True
    ie.Navigate('http://www.Rhodia-ecommerce.com/')
    while ie.Busy:
        time.sleep(1)

    def printframes():
        pythoncom.CoInitializeEx(0)
        document = ie.Document
        frames = document.getElementsByTagName(u'frame')
        for frame in frames:
            obj = frame.contentWindow

    thr = threading.Thread(target=printframes)
    thr.start()
    thr.join()

if __name__ == '__main__':
    thr = threading.Thread(target=main)
    thr.start()
    thr.join()

frame.contentWindow之前,一切都很好。 然后bam:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\python22\lib\threading.py", line 414, in __bootstrap
    self.run()
  File "C:\python22\lib\threading.py", line 402, in run
    apply(self.__target, self.__args, self.__kwargs)
  File "testie.py", line 42, in printframes
    obj = frame.contentWindow
  File "C:\python22\lib\site-packages\win32com\client\__init__.py", line 455, in __getattr__
    return self._ApplyTypes_(*args)
  File "C:\python22\lib\site-packages\win32com\client\__init__.py", line 446, in _ApplyTypes_
    return self._get_good_object_(
com_error: (-2147467262, 'No such interface supported', None, None)

任何提示?

正確的答案是手工編組。 這不是一個解決方法,它是你應該在這里做的。 您不應該使用公寓線程。

您初始化為多線程單元 - 告訴COM 可以在任何線程上調用您的接口 不允許你調用任何線程其他接口,或者從編組由COM提供的接口為你開脫。 這只會“偶然”起作用 - 例如,如果您調用的對象恰好是進程內MTA對象,則無關緊要。

CoMarshalInterThreadInterfaceInStream / CoGetInterfaceAndReleaseStream完成業務。

這樣做的原因是對象可以提供自己的代理,這些代理可能是也可能不是自由線程的。 (或者確實提供自定義編組)。 你必須整理他們告訴他們他們在線程之間移動。 如果代理是自由線程的,則可能會返回相同的指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM