The implementation of Python that we are using, CPython, has a mechanism called global interpreter lock (GIL). GIL's purpose is to make CPython thread-safe; it works by preventing the code from being executed by more than one thread at once.
With that limitation, multiprocessing in Python works by forking the running program (for example, making a copy of the state of the program) and sending it to another computer core. As a consequence, the new process comes with an overhead.
Let's try a simple code:
First, make a copy of the previous chapter folder in your
geopy
project and rename it toChapter10
.Clean the
Chapter10/output
folder (delete all files in it).Expand the
Chapter10/experiments
folder, right-click on it, and create a new Python file. Name itparallel.py
.Add this code to this new file:
# coding=utf-8 from datetime import datetime import multiprocessing as mp def an_expensive_function(text): for i in range(500): out = "{} {} {}" out.format...