Home >Backend Development >Python Tutorial >Why Does Python Multiprocessing Fail with 'PicklingError: Can't pickle ' and How Can I Fix It?
When attempting to utilize Python's multiprocessing module, you may encounter the "PicklingError: Can't pickle
Understanding Pickling
Pickling is a process that converts an object into a binary stream, allowing it to be stored and later recreated. In multiprocessing, tasks are transmitted over the network via a queue, necessitating that they be picklable.
Functions and Top-Level Definition
Regular functions defined at the top level of a module are inherently picklable. However, functions defined within a class or other nested function are not. This is because the "globals" encountered during pickling may not be the same as those in the process that created them.
Example of a Non-Picklable Function
Consider the following code that defines a function nested within a class method:
class Foo: def g(self): def h(): pass h()
Calling pool.apply_async(Foo().g) will result in the "PicklingError" because the nested function h is not defined at the top level.
Solution
To resolve this issue, simply define the function at the top level of the module and call it from within the nested function. For example:
def g(foo): def h(): pass h() foo.work()
Now, pool.apply_async(g, args=(Foo(),)) will function correctly.
The above is the detailed content of Why Does Python Multiprocessing Fail with 'PicklingError: Can't pickle ' and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!