Multiprocessing in Python involves starting separate processes, much like forking. This gets around the GIL and its effect on multiple threads, but you have to deal with the overhead of increased memory usage and the multiple instances of the Python interpreter that are spawned for all the processes. However, in multi-core systems, multiprocessing can take advantage of the different CPUs so you have true parallelism; more cores = more processing power.
As there isn't room to cover everything about parallel Python programming (there are entire books written on the subject), I'm going to finish this chapter by demonstrating how to automate multiprocessing using Pool()
, which controls worker processes automatically. Pool()
accepts a number of input arguments, probably the most important one being the number of processes. By default, Pool()
uses all the available CPUs on your system. This is useful because, if your system is upgraded, your program will automatically...