Python multiprocessing library
The multiprocessing
module is Python's standard library that is shipped with Python binaries, and it is available from Python 2.6. There's also the threading
module, which allows you to spawn multiple threads, but they all share the same memory space. Multiprocessing comes with more advantages than threading. One of them is isolated memory space for each process, and it can take advantage of multiple CPUs and cores.
Getting started with multiprocessing
First, you need to import the module for your Python script:
import multiprocessing as mp
Then, wrap your code with a Python function; this will allow the process to target this function and mark it as a parallel execution.
Let's suppose that we have code that connects to the router and executes commands on it using the netmiko
library, and we want to connect to all of the devices in parallel. This is a sample serial code that will connect to each device and execute the passed command, and then continue with the second...