推荐学习书目
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 · Jun 7, 2018 · 4694 views
    This topic created in 2918 days ago, the information mentioned may be changed or developed.

    主要代码是这样的,采用多线程调用 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 replies    2018-06-11 12:31:29 +08:00
    doumeki
        1
    doumeki  
       Jun 8, 2018
    好熟悉的错误...
    N 年前搞 Eexcel 学习的时候遇到过, 使用 CoInitializeEx 和 marsha 试下.

    参考: https://github.com/doumeki/ThrExcel/blob/master/ThrExcel.py 新人代码,高手勿笑.
    yeyu1989
        2
    yeyu1989  
    OP
       Jun 11, 2018
    @doumeki 受教了,虽然我还没看懂。谢谢大神~~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   928 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 21:40 · PVG 05:40 · LAX 14:40 · JFK 17:40
    ♥ Do have faith in what you're doing.