Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 12.5 — multiprocessing
Python · Example 2 of 2

What can and cannot be pickled

The program writes a small module, tools.py, and imports it, because pickle finds functions by module and name. Print the error type names only: the messages contain memory addresses.

multiprocessing runs your Python code in separate processes, each with its own interpreter, its own GIL and its own memory, so CPU-bound work can use every core. The price is that arguments and results are copied by pickling, and your script must be safe to import again.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a top-level function cube(n) and show that pickle.loads(pickle.dumps(cube)) returns the same function. (In a script run as python file.py.)

02

Use functools.partial to make a picklable version of lambda x: pow(x, 3) and call it on 4 after a pickle round trip.

03

Write a function is_picklable(obj) that returns True or False, and test it on a list and on a threading.Lock().

Explain it without notes

01

How is a process different from a thread, and why does multiprocessing get around the GIL?

02

Why must arguments to a worker process be picklable, and which common things are not?

03

What is the if __name__ == "__main__": guard for in multiprocessing code?

04

Explain spawn, fork and forkserver, and say which defaults apply.

05

A function running inside pool.map raises ValueError. What does the parent program see, and how does the rest of the pool behave?

What can and cannot be pickled
Sign in to run this example in your browser.

Expected output

same function after the round trip: True
partial works: 1024
lambda -> PicklingError
local function -> AttributeError
lock -> TypeError
original: ['prompt 1', 'prompt 2']
copy:     ['prompt 1', 'prompt 2', 'added in the copy']