Sometimes, we want to fix Python multiprocessing PicklingError: Can’t pickle function error.
In this article, we’ll look at how to fix Python multiprocessing PicklingError: Can’t pickle function error.
How to fix Python multiprocessing PicklingError: Can’t pickle function error?
To fix Python multiprocessing PicklingError: Can’t pickle function error, we should only run pickle in top level functions.
For instance, we write
import multiprocessing as mp
# ...
def work(foo):
foo.work()
if __name__ == '__main__':
pool = mp.Pool()
pool.apply_async(work, args=(foo,))
pool.close()
pool.join()
to define the work
top level function,
And then we call pool.apply_async
with work
and the args
arguments to run work
which runs foo.work
, which runs the pickle code.
Conclusion
To fix Python multiprocessing PicklingError: Can’t pickle function error, we should only pickle top level functions.