主要代码是这样的,采用多线程调用 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)
1
doumeki 2018-06-08 13:25:33 +08:00
好熟悉的错误...
N 年前搞 Eexcel 学习的时候遇到过, 使用 CoInitializeEx 和 marsha 试下. 参考: https://github.com/doumeki/ThrExcel/blob/master/ThrExcel.py 新人代码,高手勿笑. |