簡體   English   中英

TypeError:無法在 python 3 中腌制 _thread.RLock 對象

[英]TypeError: can't pickle _thread.RLock objects in python 3

我有一個非常大的使用 Flask 和 Python 的 Web API 項目。 它用於自動測試一些電子硬件。 該程序使用一些線程來運行 Web UI,而服務器運行一些服務(SSH、串行、VISA)等等。

該程序最初是在 python 2.7 中編碼的,並且在這個版本中工作得很好。 現在,出於顯而易見的原因,我正在嘗試將其更新為 python 3.8。

在更新項目時,我遇到了復制庫的問題。 它應該序列化一個_thread.RLock對象並將其發送到另一個線程,但它一直給我一個錯誤。 這是我得到的回溯:

Traceback (most recent call last):
  File "c:\git_files\[...]\nute\route_config\flask_api_testbench.py", line 208, in _hook_run
    super(FlaskAPITestbench, self).hook_run()
  File "c:\git_files\[...]\nute\core\testbench\base.py", line 291, in hook_run
    while self.state_machine():
  File "c:\git_files\[...]\nute\core\testbench\base.py", line 304, in state_machine
    on_input=self.state_testrun
  File "c:\git_files\[...]\nute\core\testbench\base.py", line 380, in wait_for_input_or_testrun
    self.hook_load_testrun(config_with_input)
  File "c:\git_files\[...]\nute\core\testbench\base.py", line 428, in hook_load_testrun
    self.interface.load_testrun(self.load_testrun(config))
  File "c:\git_files\[...]\nute\core\testbench\base.py", line 461, in load_testrun
    testrun = self.test_loader.load_testrun(config, context_type=self.TestRunContext)
  File "c:\git_files\[...]\nute\core\testrun\loader.py", line 89, in load_testrun
    testrun_template = process_all_loaders(self.batchers, _process_batcher)
  File "c:\git_files\[...]\nute\core\config\loader.py", line 127, in process_all_loaders
    return fn(loader)
  File "c:\git_files\[...]\nute\core\testrun\loader.py", line 85, in _process_batcher
    batcher.batch_testrun(testrun_template, config, context)
  File "c:\git_files\[...]\nute\batcher\python_module_batcher.py", line 21, in batch_testrun
    batch_module.main(testrun, context)
  File "C:\GIT_Files\[...]\pyscripts\script\patest\_batch.py", line 168, in main
    test.suite(ImpedanceTest)
  File "c:\git_files\[...]\nute\core\testrun\base.py", line 213, in suite
    testsuite = testsuite_instance_or_class()
  File "c:\git_files\[...]\nute\core\functions\helpers.py", line 233, in __new__
    cls._attach_nodes_to(template)
  File "c:\git_files\[...]\nute\core\functions\helpers.py", line 271, in _attach_nodes_to
    node = root.import_testcase(testcase)
  File "c:\git_files\[...]\nute\core\functions\specific.py", line 307, in import_testcase
    test_node = testcase.copy(cls=self.__class__)
  File "c:\git_files\[...]\nute\core\functions\base.py", line 645, in copy
    value = copy(value)
  File "c:\users\[...]\.conda\envs\py37\lib\copy.py", line 96, in copy
    rv = reductor(4)
TypeError: can't pickle _thread.RLock objects

它在 Python 2.7 中運行良好,但不適用於 Python 3.x。 我已經在 3.7.10、3.8.9 和 3.9.6 上嘗試過,結果相同。

這是我的 wrap復制方法的實現:

from copy import copy

...

def copy(self, cls=None): # class method
    if cls is None:  
        cls = self.__class__
    new_self = cls()
    for key, value in self.__dict__.items():
        # if key == "multithread_lock":
        #     continue

        if self.should_copy_attribute(key, value):
            # Handle recursion by pointing to the new object instead of copying.
            if value is self:
                value = new_self
            else:
                value = copy(value)  # This is where it fails
            new_self.__dict__[key] = value
    return new_self

正如您在注釋部分看到的那樣,跳過任何 _thread.RLock 對象的酸洗可以使程序工作,但我需要手動刷新 Web UI 才能看到它正在運行,因為線程不起作用。

知道為什么它適用於 python 2.7 但不適用於較新版本嗎? 提前致謝。

所以我發現無法復制 _thread.RLock() 對象。 我剛剛添加了一個條件來跳過要復制的這樣的對象,它工作正常。 對於 web UI 不刷新,我換了低版本的 Flask-SocketIO,它工作得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM