Python 3.6 Tkinter and Multiprocessing Python 3.6 Tkinter and Multiprocessing tkinter tkinter

Python 3.6 Tkinter and Multiprocessing


What you could do is create a multiprocessing.Queue as a global variable and a separate process using multiprocessing.Popen before running the mainloop.

The function that you use in Popen (which is run in a separate process) should wait for messages on the Queue and act on them. This process does the real work.

Meanwhile the GUI feeds commands (e.g. a 2-tuple of command and username such as ('delete', 'foo@bar')) to delete a user) and data into the Queue based on user input. It should also use an after callback to query the queue and process any responses from the other process.

Some notes on style.

  • It is conventional in Python to start class names with an uppercase letter. Function and method names should be lower case. See PEP8.
  • Since you are running a GUI, it would be better style to use message boxes to inform the user about errors instead of print statements. Depending on the OS and how the program is started the user might or might not see the printed output.
  • It is generally not considered good practice to use from <module> import *. In this case I would suggest import tkinter as tk.


Ok, So I was able to get the processing working thanks to some online articles and Roland Smith. Unfortunately to get it to work with tkinter, I had to remove the button that sends the function for the user and just have it automatically perform it between GUI's.

Currently the username/password are not passing, I know for them to pass I probably need to put them in the Process args part of my loop so they're passed to each Process (which I haven't tested yet).

I could use some more direction on getting Queuing stood up within my for loop, and passing anything that happens within the processes to my final output GUI (root).

def start_commands(vpntermination, a_device):  print('VPN Termination Commands are in process for User: ' + str(vpntermination) +'....please wait.')  print('')  start_time = datetime.now()  for a_device in all_firewalls: #Maybe add threading/processing into the for loop?    per_fw_start_time = datetime.now()    try:      net_connect = ConnectHandler(**a_device)      hostname = net_connect.send_command("show hostname") #Used to store Hostname.      output = net_connect.send_command("vpn-sessiondb logoff name " + str(vpntermination) +  " noconfirm") #Used for vpn termination command.      print('\n******* Output for device ' + hostname + ' *******' )      print(output)      print('')      per_fw_end_time = datetime.now()      per_fw_total_time = per_fw_end_time - per_fw_start_time      print("******* " + hostname + " {0} *******".format(a_device['ip'])+ " took " + str(per_fw_total_time) + " to process.")      print('')      net_connect.disconnect() #Hoping to speed up process time within multi-threading issues.    except (NetMikoTimeoutException, NetMikoAuthenticationException) as e: #Handles timeout errors.      print("Could not connect to {}, due to {}", e)      print('')      net_connect.disconnect() # Disconnect session if exception is thrown  end_time = datetime.now()  total_time = end_time - start_time  print('\nTotal process time: ' + str(total_time))def main():  username = input('Username: ')  password = getpass.win_getpass(prompt='Password: ', stream=None)  #tkinter GUI that adds user for termination.  master = Tk()  my_gui = InputWindowGUI(master)  master.mainloop() #Move the loops below root tkinter and reenable the stdredirector.  procs = []  for a_device in all_firewalls:    print(a_device)    print(vpntermination)    my_proc = Process(target=start_commands, args=(a_device, vpntermination))    my_proc.start()    procs.append(my_proc)  for a_proc in procs:    print (a_proc)    a_proc.join()  #tkinter GUI that starts the user being terminated on all VCs.  root=Tk()  out_gui = OutputWindowGUI(root)  root.mainloop()if __name__ == '__main__':  main()