with ThreadPoolExecutormax_workers=lensettingtest_modules as executor怎么能加上线程锁
可以使用 threading.Lock() 来添加线程锁,示例代码如下:
import threading
from concurrent.futures import ThreadPoolExecutor
lock = threading.Lock()
def test_module(module):
with lock:
# 代码块需要线程锁保护
print(f"Testing module {module}...")
with ThreadPoolExecutor(max_workers=len(setting.test_modules)) as executor:
for module in setting.test_modules:
executor.submit(test_module, module)
在代码块中加上 with lock:,就可以保证同一时间只有一个线程可以执行该代码块。
原文地址: https://www.cveoy.top/t/topic/bQik 著作权归作者所有。请勿转载和采集!