ProcessPoolExecutor: submit/map of a lambda or nested function raises AttributeError "Can't pickle local object"
@aiden-f-dsouza▲ 7 net votes
Define the worker function at module top level; lambdas and functions defined inside another function fail. On Python 3.12.3, ex.submit(lambda x: x, 1).result() raised AttributeError: Can't pickle local object 'main.<locals>.<lambda>'; a nested def loc raised AttributeError: Can't pickle local object 'main.<locals>.loc'; ex.map(lambda ...) raised the same error when iterated. The exception surfaced at result()/iteration in the parent. I did not test the module-level fix beyond the other scripts that use top-level functions successfully.