我一直在寻找以下多处理问题的解决方案。
我在 record.py 模块中有一个 Record 类。记录类的职责是处理输入数据并将其保存到 JSON 文件中。Record 类有 put() 方法来更新 JSON 文件。
记录类在类装饰器中初始化。装饰器应用于各个子模块的大多数类。Decorator 提取它所装饰的每个方法的信息,并将数据发送到 Record 类的 put() 方法。Record 类的 put() 方法然后更新 JSON 文件。
问题是当不同的进程运行时,每个进程都会创建自己的记录对象实例,并且由于多个进程尝试更新相同的 json 文件,Json 数据会被损坏。此外,每个进程可能都有运行的线程尝试访问和更新相同的 JSON 文件。请让我知道如何解决这个问题。
class Record():
def put(data):
# read json file
# update json file with new data
# close json file
def decorate_method(theMethod):
# Extract method details
data = extract_method_details(theMethod)
# Initialize record object
rec = Record()
rec.put(data)
class ClassDeco(cls):
# This class decorator decorates all methods of the target class
for …Run Code Online (Sandbox Code Playgroud) 从 Excel 工作表生成 pdf 时,我收到以下错误:
ws.ExportAsFixedFormat(0, save_as)
File "<COMObject <unknown>>", line 5, in ExportAsFixedFormat
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024809), None)
Run Code Online (Sandbox Code Playgroud)
下面是我的代码:
pythoncom.CoInitialize()
xlApp = client.Dispatch("Excel.Application")
logging.debug("Saving excel file {} to file {}".format(filename, save_as))
books = xlApp.Workbooks.Open(filename)
ws = books.Worksheets[0]
ws.Visible = 1
ws.ExportAsFixedFormat(0, save_as)
books.Close(True)
xlApp.Quit()
Run Code Online (Sandbox Code Playgroud)
它在我安装了 Office 365 的笔记本电脑上运行,但在安装了 Microsoft Office 2007 的另一个系统上出现上述错误。
Python版本:python 2.7
openpyxl:2.4.5
pywin32:224
没有足够的文档。如果有人可以提供调试它并理解错误的指针,那将会有很大的帮助。