繁体   English   中英

多线程 python

[英]Multi Threading python

我正在尝试根据我提供的 IP 列表优化连接到某些路由器的脚本。

但是,一旦完成前一个路由器就开始配置一次,完成配置需要很长时间,我的问题是,是否可以创建一个不需要等待的 function,也许是一个多线程 function可以同时配置几个元素。

def configure_routers():

    hosts = create_vrdc_dic()  # -> it's the dictonary that pass the variables

    for i in hosts .keys():
        hostname = hosts [i].get('hostname')
        mgmt_ip = hosts [i].get('mgmt_ip')

        console_config.config_vrdc(hostname, mgmt_ip)  # -> here I'm calling another module that configures the routers using pexpect

感谢您在这里的任何帮助。

您可能想要使用多处理模块:

##################################################

# 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()

可能的 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]

运行时间:~10 秒

要同时配置您的路由器,您首先需要确保使用您正在使用的模块/库这样做是安全的。 如果一次只运行一个实例,您只需等待每台路由器都配置好。 例如,如果您正在使用的进程在运行时读取/写入同一文件/数据库,并且您对该进程进行多线程处理,则最终可能会导致文件/数据库损坏。

如果您确定您的操作对多线程安全的,请尝试以下代码:

import threading


# Create a class for multi-threading the configuration of a router
class RouterConfigure(threading.Thread):
    def __init__(self, ip: str, hostname: str):
        threading.Thread.__init__(self)
        self.ip = ip
        self.hostname = hostname

    def run(self):
        console_config.config_vrdc(self.hostname, self.ip)


threads = []
hosts = create_vrdc_dic()

# Create a thread for each IP/hostname in the dict
for i in hosts.keys():
    hostname = hosts[i].get('hostname')
    ip = hosts[i].get('mgmt_ip')
    
    thread = RouterConfigure(ip=ip, hostname=hostname)
    thread.start()
    threads.append(thread)

# Wait for threads to finish before exiting main program
for t in threads:
    t.join()

试试这个代码,请根据您的需要进行必要的更改。

import thread

def Configure (hostname, mgmt_ip):
    
    console_config.config_vrdc(hostname, mgmt_ip)


try :
    def configure_routers():

    hosts = create_vrdc_dic()  # -> it's the dictonary that pass the variables

    for i in hosts .keys():
        hostname = hosts [i].get('hostname')
        mgmt_ip = hosts [i].get('mgmt_ip')

        thread.start_new_thread( Configure, (hostname, mgmt_ip) )

    configure_routers()
except :
    print("The operation cannot be performed")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM