14.4 Using a multiprocessing pool for concurrent processing
One elegant way to make use of the multiprocessing
module is to create a processing Pool
object and assign work to the various workers in that pool. We will depend on the OS to interleave execution among the various processes. If each of the processes has a mixture of I/O and computation, we should be able to ensure that our processor (and disk) are kept very busy. When processes are waiting for the I/O to complete, other processes can do their computations. When an I/O operation finishes, the process waiting for this will be ready to run and can compete with others for processing time.
The recipe for mapping work to a separate process looks like this:
def demo_mp(root: Path = SAMPLE_DATA, pool_size: int | None = None) -> None:
pool_size = (
multiprocessing.cpu_count() if pool_size is None
&...