使用多处理时尝试追加到列表

时间:2019-02-11 22:42:58

标签: python python-multiprocessing

我已经开始使用Python和Netmiko连接到Cisco设备并将CLI命令抓取到数据中。这很好用,但是当设备数量很多时,我需要减少完成脚本所需的时间。

只要我在正在使用的函数中打印输出,我就可以使多处理工作。但是,当我尝试将共享列表与多处理一起使用时,它将失败。

在此示例中,我在“ devices.py”中生成了一个名为“ device_list”的词典列表:

device_list = []
ip = 211

for x in range(5):
    device_list.append({'host': '192.168.0.' + str(ip),
                       'username': 'myusername',
                       'password': 'mypassword',
                       'device_type': 'cisco_ios'})
    ip = ip + 1

Netmiko模块将使用此词典列表连接到我的测试实验室中的设备。

这是主程序:

import multiprocessing as mp
from time import time
from netmiko import Netmiko
from devices import device_list

def connect_to_dev(output, **device):
    net_connect = Netmiko(**device)
    show_ver = net_connect.send_command('show version', use_textfsm = True)
    net_connect.disconnect()
    hostname = show_ver[0]['hostname']
    image = show_ver[0]['running_image'][28:62]
    result = '{}\nHostname: {}, version {}'.format(line, hostname, image)
    output.append(result)

if __name__ == '__main__':
    line = '*'*70
    start = time()
    mgr = mp.Manager()
    output = mgr.list()
    processes = []

    for device in device_list:
        p = mp.Process(target=connect_to_dev, args=output, kwargs=device)
        processes.append(p)
        p.start()
    for p in processes:
        p.join()

    end = time()
    runtime = round((end - start), 2)
    print(output)
    print('{}\nTime elapsed: {} seconds\n{}'.format(line, runtime, line))

我试图将由“ connect_to_dev”函数生成的每个“结果”附加到“输出”列表中。我收到此错误:

Process Process-2:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
TypeError: connect_to_dev() missing 1 required positional argument: 'output'
Process Process-3:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
Process Process-5:
TypeError: connect_to_dev() missing 1 required positional argument: 'output'
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
TypeError: connect_to_dev() missing 1 required positional argument: 'output'
Process Process-6:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
TypeError: connect_to_dev() missing 1 required positional argument: 'output'
Process Process-4:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
TypeError: connect_to_dev() missing 1 required positional argument: 'output'
[]
**********************************************************************
Time elapsed: 0.05 seconds
**********************************************************************

0 个答案:

没有答案