赞
踩
因为太多的博客并没有解决多进程问题,本文是自己学习后加入自己深入理解的总结记录,方便自己以后查看。
尝试了许多方式以及网上的资料发现调用pthon脚本不能创建multiprocessing的进程的原因以及解决方案,在下面的内容中写出,如果哪位大佬有解决方案请留言
[' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\testxbb1111.py", line 19, in test2\n project = pickle.load(open(\'D:\\AllLargeProject\\Visontool\\project3.vtpro\', \'rb\'))\n', ' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\core\\project.py", line 141, in __setstate__\n self._shared_memory = DataManager()\n', ' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\core\\datashared.py", line 251, in __init__\n self._manager = multiprocessing.Manager()\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\context.py", line 56, in Manager\n m.start()\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\managers.py", line 513, in start\n self._process.start()\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\process.py", line 105, in start\n self._popen = self._Popen(self)\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\context.py", line 322, in _Popen\n return Popen(process_obj)\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\popen_spawn_win32.py", line 48, in __init__\n None, None, False, 0, None, None, None)\n'] 在 Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)
原因:
- 我们使用pythonnet调用时,是已经在C#开启的程序入口中的主进程或子进程中运行python脚本,无法开启python程序的 if name ==‘main’: 程序入口,要实现python 的多进程和多线程,if name ==‘main的程序入口这个功能不可或缺,所以导致不能开启多进程和多线程
解决方法:
- 1、先利用序列化和反序列化pickle来封装自己项目文件,在C#中利用pickle来反序列化我们的项目使项目在初始化时直接进__setstate__(self,state)函数来更新项目中的参数,使项目的入口setstate来做程序的入口调用项目
- 2、在进程中添加recover(),在share memory中设定进程开启标志,通过标志来分享内存
[' File "D:\\AllLargeProject\\Visontool\\windows版本VT\\vt_windows_software_1.3.7.0\\testxbb1111.py", line 20, in test2\n project = pickle.load(open(\'D:\\AllLargeProject\\Visontool\\windows4.vtpro\', \'rb\'))\n', '
File "visiontool\\core\\project.py", line 250, in visiontool.core.project.Project.__setstate__\n', '
File "visiontool\\core\\datashared.py", line 253, in visiontool.core.datashared.DataManager.__init__\n', '
File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\context.py", line 56, in Manager\n m.start()\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\managers.py", line 513, in start\n self._process.start()\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\process.py", line 105, in start\n self._popen = self._Popen(self)\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\context.py", line 322, in _Popen\n return Popen(process_obj)\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\multiprocessing\\popen_spawn_win32.py", line 48, in __init__\n None, None, False, 0, None, None, None)\n'] 在 Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)
程序在C#導入時关闭进程的开启,在程序导入project时开启进程
[' File "D:\\AllLargeProject\\Visontool\\windows版本VT\\vt_windows_software_1.3.7.0\\testxbb1111.py", line 6, in <module>\n from visiontool.tools.tools.base import ImageToolbase\n', ' File "D:\\AllLargeProject\\Visontool\\windows版本VT\\vt_windows_software_1.3.7.0\\visiontool\\__init__.py", line 11, in <module>\n from . import tools\n', ' File "D:\\AllLargeProject\\Visontool\\windows版本VT\\vt_windows_software_1.3.7.0\\visiontool\\tools\\__init__.py", line 70, in <module>\n reloadAll()\n', ' File "D:\\AllLargeProject\\Visontool\\windows版本VT\\vt_windows_software_1.3.7.0\\visiontool\\tools\\__init__.py", line 45, in reloadAll\n mod = importlib.import_module(modname)\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\importlib\\__init__.py", line 126, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n', ' File "visiontool\\tools\\detection\\aiocr.py", line 20, in init visiontool.tools.detection.aiocr\n', ' File "visiontool\\core\\common\\globalvariables\\paddleocrhelper.py", line 14, in init visiontool.core.common.globalvariables.paddleocrhelper\n', ' File "D:\\AllLargeProject\\Visontool\\windows版本VT\\vt_windows_software_1.3.7.0\\paddleocr\\__init__.py", line 16, in <module>\n from .paddleocr import PaddleOCR\n', ' File "D:\\AllLargeProject\\Visontool\\windows版本VT\\vt_windows_software_1.3.7.0\\paddleocr\\paddleocr.py", line 28, in <module>\n from tools.infer import predict_system\n', ' File "D:\\AllLargeProject\\Visontool\\windows版本VT\\vt_windows_software_1.3.7.0\\visiontool\\tools\\__init__.py", line 70, in <module>\n reloadAll()\n', ' File "D:\\AllLargeProject\\Visontool\\windows版本VT\\vt_windows_software_1.3.7.0\\visiontool\\tools\\__init__.py", line 45, in reloadAll\n mod = importlib.import_module(modname)\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\importlib\\__init__.py", line 126, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n', ' File "visiontool\\tools\\detection\\barcodeidentify.py", line 39, in init visiontool.tools.detection.barcodeidentify\n'] 在 Python.Runtime.Runtime.CheckExceptionOccurred()
#删掉aiocr
[' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\testxbb1111.py", line 19, in test2\n project = pickle.load(open(\'D:\\AllLargeProject\\Visontool\\project3.vtpro\', \'rb\'))\n', ' File
"D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\core\\project.py", line 20, in <module>\n from visiontool.core.task import Task\n', ' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\core\\task.py", line 11, in <module>\n import visiontool.core.blocks as blocks\n', ' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\core\\blocks\\__init__.py", line 25, in <module>\n from visiontool.core.blocks.device.databaseblock import DatabaseBlock\n', ' File
"D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\core\\blocks\\device\\databaseblock.py", line 13, in <module>\n from visiontool.devicetool.database import getClass, getToolLists\n', ' File
"D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\devicetool\\database\\__init__.py", line 10, in <module>\n from visiontool.devicetool.database.write import WriteTool\n', ' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\devicetool\\database\\write.py", line 11, in <module>\n from visiontool.core.device.adapter.sqlitedatabase_adapter import DBDataType\n', ' File
"D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\core\\device\\adapter\\sqlitedatabase_adapter.py", line 17, in <module>\n import sqlite3\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\sqlite3\\__init__.py", line 23, in <module>\n from sqlite3.dbapi2 import *\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\sqlite3\\dbapi2.py", line 27, in <module>\n from _sqlite3 import *\n'] 在 Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)
#将sqlite3.dll放到文件中
[' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\testxbb1111.py", line 19, in test2\n project = pickle.load(open(\'D:\\AllLargeProject\\Visontool\\project3.vtpro\', \'rb\'))\n', ' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\__init__.py", line 11, in <module>\n from . import tools\n', ' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\tools\\__init__.py", line 68, in <module>\n reloadAll()\n', ' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\tools\\__init__.py", line 45, in reloadAll\n mod = importlib.import_module(modname)\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\importlib\\__init__.py", line 126, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n', ' File "D:\\AllLargeProject\\Visontool\\Linux\\VisionTool_PY\\visiontool\\tools\\detection\\barcodeidentify.py", line 15, in <module>\n from pyzbar.pyzbar import decode\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\site-packages\\pyzbar\\pyzbar.py", line 7, in <module>\n from .wrapper import (\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\site-packages\\pyzbar\\wrapper.py", line 148, in <module>\n c_uint_p, # minor\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\site-packages\\pyzbar\\wrapper.py", line 141, in zbar_function\n return prototype((fname, load_libzbar()))\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\site-packages\\pyzbar\\wrapper.py", line 120, in load_libzbar\n libzbar, dependencies = zbar_library.load()\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\site-packages\\pyzbar\\zbar_library.py", line 60, in load\n dependencies, libzbar = load_objects(Path(__file__).parent)\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\site-packages\\pyzbar\\zbar_library.py", line 52, in load_objects\n for dep in dependencies\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\site-packages\\pyzbar\\zbar_library.py", line 52, in <listcomp>\n for dep in dependencies\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\ctypes\\__init__.py", line 426, in LoadLibrary\n return self._dlltype(name)\n', ' File "C:\\Users\\xbb1995\\anaconda3\\envs\\visiontool\\Lib\\ctypes\\__init__.py", line 348, in __init__\n self._handle = _dlopen(self._name, mode)\n'] 在 Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)
在 Python.Runtime.PyObject.InvokeMethod(String name, PyTuple args, PyDict kw)
#卸载visiontool环境中的pyzbar(pip uninstall pyzbar),因为这里要使用本地文件中的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。