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

因为调用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:UsersgxwangzhAppDataLocalProgramsPythonPython39libsite-packagesxlwings\_xlwindows.py", line 135, in __getattr__
    v = getattr(self._inner, item)
  File "C:UsersgxwangzhAppDataLocalProgramsPythonPython39libsite-packageswin32comclient\__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 FilesJetBrainsPyCharm Community Edition 2021.1.1pluginspython-cehelperspydev\_pydev_bundlepydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:Program FilesJetBrainsPyCharm Community Edition 2021.1.1pluginspython-cehelperspydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"
", file, exec), glob, loc)
  File "D:/Software/pythonProject/test.py", line 20, in <module>
    thread_1.start()
  File "C:UsersgxwangzhAppDataLocalProgramsPythonPython39libmultiprocessingprocess.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:UsersgxwangzhAppDataLocalProgramsPythonPython39libmultiprocessingcontext.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:UsersgxwangzhAppDataLocalProgramsPythonPython39libmultiprocessingcontext.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:UsersgxwangzhAppDataLocalProgramsPythonPython39libmultiprocessingpopen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)
  File "C:UsersgxwangzhAppDataLocalProgramsPythonPython39libmultiprocessing
eduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "C:UsersgxwangzhAppDataLocalProgramsPythonPython39libsite-packagesxlwings\_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:UsersgxwangzhAppDataLocalProgramsPythonPython39libmultiprocessingspawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:UsersgxwangzhAppDataLocalProgramsPythonPython39libmultiprocessingspawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
经验分享 程序员 微信小程序 职场和发展