我有一個非常大的專案,是一個使用Flask和Python的Web API。它被用于自動測驗一些電子硬體。 該程式使用了一些執行緒,以便在服務器運行一些服務(SSH、串行、VISA)的同時運行一個Web UI。
該程式最初是在Python 2.7中編碼的,在這個版本中作業得很好。現在,由于明顯的原因,我正試圖將其更新到 python 3.8。
當我更新專案時,我在copy庫中遇到了麻煩。它應該序列化一個_thread.RLock物件并將其發送到另一個執行緒,但它一直給我一個錯誤。以下是我得到的跟蹤結果:
traceback (most recent call last):
檔案 "c:git_files[...] 。
ute
oute_configflask_api_testbench.py", line 208, in _hook_run
super(FlaskAPITestbench, self).hook_run()
檔案 "c:git_files[...] 。
utecore estbenchase.py",第291,in hook_run
while self.state_machine()。
檔案"c:git_files[...]
utecore estbenchase.py", 行 304, in state_machine
on_input=self.state_testrun
檔案"c:git_files[...]
utecore estbenchase.py", 行 380, in wait_for_input_or_testrun
self.hook_load_testrun(config_with_input)
檔案"c:git_files[...]
utecore estbenchase.py", 行 428, in hook_load_testrun
self.interface.load_testrun(self.load_testrun(config))
檔案"c:git_files[...]
utecore estbenchase.py", 行 461, in load_testrun
testrun = self.test_loader.load_testrun(config, context_type=self.TestRunContext)
檔案 "c:git_files[...] 。
utecore estrunloader.py", line 89, in load_testrun
testrun_template = process_all_loaders(self.batchers, _process_batcher)
檔案"c:git_files[...]
utecoreconfigloader.py", 行 127, in process_all_loaders
return fn(loader)
檔案 "c:git_files[...] 。
utecore estrunloader.py", 行 85, in _process_batcher
batcher.batch_testrun(testrun_template, config, context)
檔案 "c:git_files[...] 。
uteatcherpython_module_batcher.py", 行 21, in batch_testrun
batch_module.main(testrun, context)
檔案 "C:GIT_Files[...]pyscriptspatest_batch.py", line 168, in main
test.suite(ImpedanceTest)
檔案"c:git_files[...]
utecore estrunase.py", 行 213, in suite
testuite = testsuite_instance_or_class()
檔案 "c:git_files[...] 。
utecorefunctionshelpers.py", 行 233, in __new__
cls._attach_nodes_to(template)
檔案 "c:git_files[...] 。
utecorefunctionshelpers.py", 行 271, in _attach_nodes_to
node = root.import_testcase(testcase)
檔案 "c:git_files[...] 。
utecorefunctionsspecific.py", 行 307, in import_testcase
test_node = testcase.copy(cls=self.__class__)
檔案 "c:git_files[...] 。
utecorefunctionsase.py", 行 645, in copy
value = copy(value)
檔案 "c:users[...].condaenvspy37libcopy.py", line 96, in copy
rv = reductor(4)
TypeError: can not pickle _thread.RLock objects
它在 Python 2.7 中作業正常,但在 Python 3.x 中不正常。我在 3.7.10、3.8.9 和 3.9.6 上試過,結果相同。
下面是我對copy的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)。
# 通過指向新物件而不是復制來處理遞回。
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物件的pickling使程式作業,但我需要手動重繪 Web UI以看到它的運行,因為執行緒不作業。
你知道為什么它在 python 2.7 上能作業,而在較新的版本上卻不能?預先感謝。
uj5u.com熱心網友回復:
所以我發現_thread.RLock()物件不能被復制。我只是添加了一個條件來跳過這樣的物件被復制,而且作業正常。 對于Web UI不重繪 的問題,我換了一個較低版本的Flask-CoketIO,它就能正常作業了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/309354.html
標籤:
上一篇:為多列設定類別和型別可能嗎?
