PYTHONCriticalRuntime ErrorApril 22, 2026

Runtime Error

RuntimeError: cannot pickle local function '<locals>.<lambda>'

What This Error Means

This error occurs when a function or object cannot be serialized because it is not pickleable. In this case, it is a lambda function that is being used in a multiprocessing or multithreading context.

Why It Happens

Python's multiprocessing and multithreading modules use pickling to serialize functions and objects. However, lambda functions are not pickleable by default because they are created on the fly and do not have a defined name. This can cause issues when trying to serialize and deserialize them across different processes or threads.

How to Fix It

  1. 1To fix this error, you can define a regular function instead of using a lambda function. Regular functions are pickleable and can be safely used in multiprocessing and multithreading contexts.
  2. 2// Before (broken code)
  3. 3from multiprocessing import Pool
  4. 4pool = Pool(processes=4)
  5. 5pool.map(lambda x: x**2, [1, 2, 3, 4])
  6. 6// After (fixed code)
  7. 7def square(x):
  8. 8 return x**2
  9. 9pool = Pool(processes=4)
  10. 10pool.map(square, [1, 2, 3, 4])

Example Code Solution

❌ Before (problematic code)
Python
def calculate_squares(pool_size):
    pool = Pool(processes=pool_size)
    return pool.map(lambda x: x**2, [1, 2, 3, 4])
✅ After (fixed code)
Python
def calculate_squares(pool_size):
    def square(x):
        return x**2

    pool = Pool(processes=pool_size)
    return pool.map(square, [1, 2, 3, 4])

Fix for RuntimeError: cannot pickle local function '<locals>.<lambda>'

Related PYTHON Errors

Related PYTHON Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error