使用 ThreadPool 执行器的结果使用函数的参数创建多个文件

Create multiple files using parameters from function using results from ThreadPool executor

我有一个简单的功能,可以收集多个网络设备中的卡片清单。我正在使用 executor.map 调用函数并生成结果。这一切都很好。但是,我需要将结果写入不同的文件。理想情况下,我希望为我收集清单的每个节点都有一个文件名。所以像 node1-inventory.txt, node2-inventory.txt 等等。当我循环遍历结果并将它们写入文件时,我无法弄清楚如何在文件名中包含节点名称。我的输出将是 node1-inventory.txt、node2-inventory.txt 等等。

def get_node_inventory(node_name):
    with Ta5kTelnet() as tlnt:
        tlnt.login(node_name, 'username', 'password')
        shelf_inventory = tlnt.send_command(b"show system inventory", b"#")
        return shelf_inventory

nodes = [node1, node2, node3]

with concurrent.futures.ThreadPoolExecutor() as executor:
    results = executor.map(get_node_inventory, nodes)

    for result in results:
        with open(node-name + "-inventory.txt", "w") as f:
            f.write(result)

简单地 return 一个包含节点名称和结果的元组,而不仅仅是结果。

def get_node_inventory(node_name):
    with Ta5kTelnet() as tlnt:
        tlnt.login(node_name, "username", "password")
        shelf_inventory = tlnt.send_command(b"show system inventory", b"#")
        return (node_name, shelf_inventory)

with concurrent.futures.ThreadPoolExecutor() as executor:
    results = executor.map(get_node_inventory, [node1, node2, node3])

    for (node_name, result) in results:
        with open(node_name + "-inventory.txt", "w") as f:
            f.write(result)

或者,您也可以在工作线程中写入不同的文件。我在这里切换到 multiprocessing 以使用 imap_unordered 因为这个任务不应该关心执行顺序。

from multiprocessing.pool import ThreadPool


def get_node_inventory(node_name):
    with Ta5kTelnet() as tlnt:
        tlnt.login(node_name, "username", "password")
        return tlnt.send_command(b"show system inventory", b"#")


def write_node_inventory(node_name):
    inventory = get_node_inventory(node_name)
    with open(node_name + "-inventory.txt", "w") as f:
        f.write(result)


with ThreadPool() as pool:
    for result in pool.imap_unordered(write_node_inventory, [node1, node2, node3]):
        pass