V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yeyu1989
V2EX  ›  Python

Python 多线程时,报错'应用程序调用一个已为另一线程整理的接口',折腾我一晚上了

  •  
  •   yeyu1989 · 2018-06-07 22:15:48 +08:00 · 4083 次点击
    这是一个创建于 2366 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主要代码是这样的,采用多线程调用 process,process 再去调用其它函数

    from multiprocessing.dummy import Pool as ThreadPool
    import pythoncom
    global wb
    def process(r):
        sht = wb.sheets["开户案例"]
        sht.range((r,1)).value = '正在发送账户申请...'
        result = accinfo(wb, r)  #所在行,根据这个所在行,去进行后续操作
        sht.range((r,1)).value = result
    
    pool = ThreadPool(5)
    list_args=[]
    for r in rows:
    	list_args.append(r.row)
    
    r1=pool.map(process, list_args)
    pool.close()
    pool.join()
    

    提示的报错信息比较多

    Traceback (most recent call last):
        r1=pool.map(process, list_args)
      File "C:\Users\zhangjw\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 260, in map
        return self._map_async(func, iterable, mapstar, chunksize).get()
      File "C:\Users\zhangjw\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 608, in get
        raise self._value
      File "C:\Users\zhangjw\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 119, in worker
        result = (True, func(*args, **kwds))
      File "C:\Users\zhangjw\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\pool.py", line 44, in mapstar
        return list(map(*args))
      File "C:\Users\zhangjw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\main.py", line 649, in sheets
        return Sheets(impl=self.impl.sheets)
      File "C:\Users\zhangjw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\_xlwindows.py", line 454, in sheets
        return Sheets(xl=self.xl.Worksheets)
      File "C:\Users\zhangjw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlwings\_xlwindows.py", line 116, in __getattr__
        v = getattr(self._inner, item)
      File "C:\Users\zhangjw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
        ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
    pywintypes.com_error: (-2147417842, '应用程序调用一个已为另一线程整理的接口。', None, None)
    
    2 条回复    2018-06-11 12:31:29 +08:00
    doumeki
        1
    doumeki  
       2018-06-08 13:25:33 +08:00
    好熟悉的错误...
    N 年前搞 Eexcel 学习的时候遇到过, 使用 CoInitializeEx 和 marsha 试下.

    参考: https://github.com/doumeki/ThrExcel/blob/master/ThrExcel.py 新人代码,高手勿笑.
    yeyu1989
        2
    yeyu1989  
    OP
       2018-06-11 12:31:29 +08:00
    @doumeki 受教了,虽然我还没看懂。谢谢大神~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2870 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:52 · PVG 20:52 · LAX 04:52 · JFK 07:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.