示例工程 19-threading-gil-multiprocessing

19-threading-gil-multiprocessing/05-pickle-constraint/pickle_constraint.py

python · 0.6 KB

"""演示进程池对不可 pickle 对象的限制。"""
 
from __future__ import annotations
 
import threading
from concurrent.futures import ProcessPoolExecutor
 
 
def identity(value: object) -> object:
    return value
 
 
def main() -> None:
    lock = threading.Lock()
    with ProcessPoolExecutor(max_workers=1) as pool:
        try:
            pool.submit(identity, lock).result()
        except Exception as exc:
            print(f"error: {type(exc).__name__}")
            print(f"mentions pickle: {'pickle' in str(exc).lower()}")
        else:
            print("unexpected: true")
 
 
if __name__ == "__main__":
    main()