栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在多线程中使用win32com

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在多线程中使用win32com

如果要在多个线程中使用win32com,则需要做更多的工作,因为

COMObject
不能直接传递给线程。您需要在线程之间使用
CoMarshalInterThreadInterfaceInStream()
CoGetInterfaceAndReleaseStream()
传递实例:

import pythoncom, win32com.client, threading, timedef start():    # Initialize    pythoncom.CoInitialize()    # Get instance    xl = win32com.client.Dispatch('Excel.Application')    # Create id    xl_id = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, xl)    # Pass the id to the new thread    thread = threading.Thread(target=run_in_thread, kwargs={'xl_id': xl_id})    thread.start()    # Wait for child to finish    thread.join()def run_in_thread(xl_id):    # Initialize    pythoncom.CoInitialize()    # Get instance from the id    xl = win32com.client.Dispatch( pythoncom.CoGetInterfaceAndReleaseStream(xl_id, pythoncom.IID_IDispatch)    )    time.sleep(5)if __name__ == '__main__':    start()

有关更多信息,请参见:https : //mail.python.org/pipermail/python-
win32/2008-June/007788.html



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646575.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号