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 device, and so on:

from netmiko import ConnectHandlerfrom devices import R1, SW1, SW2, SW3, SW4nodes = [R1, SW1, SW2, SW3, SW4]for device in nodes:    net_connect = ConnectHandler(**device ...

Get Hands-On Enterprise Automation with Python. now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.