title: Running into a Multithreading issue connecting to multiple devices at the same time
body: <p>I am defining the main function with <code>def get_info()</code>. This function doesn't take the arguments. This program uses <code>argumentParser</code> to parse the arguments from the command line. The argument provided is the CSV file with <code>--csv</code> option. This picks up the csv file from the current directory and read the lines each containing an IP address, logs into devices serially and runs few commands return the output and appends in the text file. When the code runs it removes the old text file from the directory and create a new output text file when executed.
  Problem: I want to achieve this using threading module so that it takes 5 devices in parallel and outputs to a file. The problem I am running is with the lock issues as the same object is being used by same process at the same time. Here the sample code I have written. The threading concept is very new to me so please understand.</p>
  
  <p>import getpass
  import csv
  import time
  import os
  import netmiko
  import paramiko
  from argparse import ArgumentParser
  from multiprocessing import Process, Queue</p>
  
  <p>def get_ip(device_ip,output_q):</p>
  
  <pre><code>        try:
              ssh_session = netmiko.ConnectHandler(device_type='cisco_ios', ip=device_row['device_ip'],
                                                   username=ssh_username, password=ssh_password)
              time.sleep(2)
              ssh_session.clear_buffer()
          except (netmiko.ssh_exception.NetMikoTimeoutException,
                  netmiko.ssh_exception.NetMikoAuthenticationException,
                  paramiko.ssh_exception.SSHException) as s_error:
              print(s_error)
  </code></pre>
  
  <p>def main():</p>
  
  <pre><code>show_vlanfile = "pool.txt"
  if os.path.isfile(show_vlanfile):
    try:
      os.remove(show_vlanfile)
    except OSError as e:
      print("Error: %s - %s." %(e.filename, e.strerror))
  
  parser = ArgumentParser(description='Arguments for running oneLiner.py')
  parser.add_argument('-c', '--csv', required=True, action='store', help='Location of CSV file')             
  args = parser.parse_args()                                                                                 
  
  ssh_username = input("SSH username: ")                                                                     
  ssh_password = getpass.getpass('SSH Password: ')                                                           
  
  with open(args.csv, "r") as file:                                                                          
      reader = csv.DictReader(file)                                                                          
      output_q = Queue(maxsize=5)                                                                            
      procs = []                                                                                             
      for device_row in reader:                                                                              
         # print("+++++ {0} +++++".format(device_row['device_ip']))                                          
           my_proc = Process(target=show_version_queue, args=(device_row, output_q))                         
           my_proc.start()                                                                                   
           procs.append(my_proc)                                                                             
  
  # Make sure all processes have finished                                                                    
      for a_proc in procs:                                                                                   
          a_proc.join()                                                                                      
  
              commands = ["terminal length 0","terminal width 511","show run | inc hostname","show ip int brief | ex una","show
  </code></pre>
  
  <p>vlan brief","terminal length 70"]<br>
                  output = ''<br>
                  for cmd in commands:<br>
                     output += "\n"<br>
                     output += ssh_session.send_command(cmd)<br>
                     output += "\n"<br>
                     with open("pool.txt", 'a') as outputfile:<br>
                     while not output_q.empty():<br>
                     output_queue = output_q.get()<br>
                     for x in output_queue:<br>
                         outputfile.write(x)                                                                     </p>
  
  <p>if <strong>name</strong> == "<strong>main</strong>":<br>
      main() </p>