【问题标题】:Is there a way to run subprocesses in parallel?有没有办法并行运行子进程?
【发布时间】:2021-10-10 13:09:11
【问题描述】:

我是 Python 新手。

我正在尝试在我的测试环境中运行以下脚本来创建一个包含打印机 IP 及其计数器的 csv 文件。 是否可以并行运行我列表中的所有 IP?

import subprocess, csv
f = open("Printer_SNMP.csv", "w", encoding='UTF8', newline="")
writer = csv.writer(f, delimiter=",")

ips = ["192.168.0.10"]
for i in range (11,25):
    ips.append("192.168.0."+str(i))

for ip in ips:
    counter = (subprocess.getoutput('snmpwalk -c public -v 2c ' + ip + ' .1.3.6.1.4.1.1347.43.10.1.1.12.1.1')).split()[-1]
    if not counter.isdigit():
        counter = "NaN"
    writer.writerow([ip, counter])    
f.close()

提前谢谢你!

【问题讨论】:

    标签: python-3.x csv parallel-processing subprocess snmp


    【解决方案1】:

    看看多处理库 (https://docs.python.org/fr/3/library/multiprocessing.html),这些正是您所需要的示例。

    【讨论】:

    • 我一个字都听不懂……因为它是法语的 ;) 不,但说真的,谢谢!我会复习的。
    • 我相信你能处理好 ;)
    猜你喜欢
    • 2017-09-02
    • 2022-06-15
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多