You might want to use the multiprocessing module:
##################################################
# Glue functions to get the original function running
import time
def create_vrdc_dic():
return {
'host1': { 'hostname': 'host1', 'mgmt_ip': '10.0.0.1' },
'host2': { 'hostname': 'host2', 'mgmt_ip': '10.0.0.2' },
'host3': { 'hostname': 'host3', 'mgmt_ip': '10.0.0.3' },
'host4': { 'hostname': 'host4', 'mgmt_ip': '10.0.0.4' },
'host5': { 'hostname': 'host5', 'mgmt_ip': '10.0.0.5' } }
class ConsoleConfig:
def config_vrdc(self, hostname, mgmt_ip):
print("Configure [%s] [%s]" % (hostname, mgmt_ip))
time.sleep(10)
print("Finished configuration [%s] [%s]" % (hostname, mgmt_ip))
console_config = ConsoleConfig()
##################################################
from multiprocessing import Process
def configure_routers():
hosts = create_vrdc_dic() # -> it's the dictonary that pass the variables
processes = []
for i in hosts .keys():
hostname = hosts [i].get('hostname')
mgmt_ip = hosts [i].get('mgmt_ip')
p = Process(target=console_config.config_vrdc, args=(hostname, mgmt_ip))
p.start()
processes.append(p)
for p in processes:
p.join()
##################################################
configure_routers()
Possible output:
Configure [host1] [10.0.0.1]
Configure [host2] [10.0.0.2]
Configure [host3] [10.0.0.3]
Configure [host4] [10.0.0.4]
Configure [host5] [10.0.0.5]
Finished configuration [host1] [10.0.0.1]
Finished configuration [host2] [10.0.0.2]
Finished configuration [host5] [10.0.0.5]
Finished configuration [host4] [10.0.0.4]
Finished configuration [host3] [10.0.0.3]
Runtime: ~10 secs