Need Assistance?

support@tutorialshub.in

Concurrency and Performance

Threading

ADVERTISEMENT
Python Free Lesson
5 min read
ADVERTISEMENT

Processes provide separate execution contexts

Multiprocessing creates separate processes with independent Python interpreter states. This can provide real CPU parallelism for CPU-heavy tasks, but process startup and inter-process data transfer have costs.

Design workers around independent data

Functions submitted to a process pool should generally receive serializable input and return serializable results. Large amounts of shared mutable state are a sign that the design may need reconsideration.

Protect process startup

Use the appropriate main-entry guard when creating process pools, especially on platforms where child processes start by importing the main module.

Practice: benchmark a CPU-heavy calculation sequentially and with a process pool using a workload large enough to justify the overhead.

ADVERTISEMENT