Python多进程写入Excel调用start()出现异常

在尝试使用多进程加速通过xlwings库写入Excel数据时,遇到进程启动异常。错误信息显示‘win32com.gen_py.MicrosoftExcel16.0ObjectLibrary._Worksheet’对象没有'__getstate__'属性。问题可能与xlwings和多进程之间的对象序列化有关。尝试使用run()函数避免异常,但失去了并发执行的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为调用xlwings输入excel数据较慢,因此希望使用多进程来加快运行速度
但实际使用下来发现Python使用多线程调用start()函数会出现异常
使用run()则OK,但是就不满足多线程同时运行的初衷了
水平有限希望有懂的大佬看下是什么异常

import xlwings as xw
import multiprocessing

class myThread(multiprocessing.Process):
    def __init__(self,  sht_res):
        multiprocessing.Process.__init__(self)
        self.sht_res = sht_res
    def run(self):
        self.sht_res.range((1, 1)).value = "1"
        print(2)

if __name__ == '__main__':
    app = xw.App(visible=True, add_book=False)
    wb = app.books.add()
    # xw.Book(os.path.join(os.getcwd(),'results.xlsx')) #参数为空时打开新工作簿
    # xw.sheets['results'].delete() # 测试时需要先删除原工作表
    sht_res = wb.sheets.add('results')  # 新建results工作表
    thread_1 = myThread(sht_res)
    thread_2 = myThread(sht_res)
    thread_1.start()

异常如下:

Traceback (most recent call last):
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\_xlwindows.py", line 135, in __getattr__
    v = getattr(self._inner, item)
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\__init__.py", line 580, in __getattr__
    raise AttributeError(
AttributeError: '<win32com.gen_py.Microsoft Excel 16.0 Object Library._Worksheet instance at 0x1254957369472>' object has no attribute '__getstate__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/Software/pythonProject/test.py", line 20, in <module>
    thread_1.start()
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\_xlwindows.py", line 154, in __getattr__
    self._oleobj_.GetIDsOfNames(0, item)
pywintypes.com_error: (-2147352570, '未知名称。', None, None)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\gxwangzh\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值