Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. from concurrent.futures import ThreadPoolExecutor
  2. from concurrent.futures import as_completed
  3. from multiprocessing import Pool
  4. import multiprocessing
  5. import json
  6.  
  7. """
  8. DO NOT USE THIS CODE IT DOES NOT WORK
  9. In my testing I was not able to consistently return data
  10. from threaded child processes. If you have information that
  11. might help to make this work please comment!
  12. """
  13.  
  14.  
  15. def metric_1_cmd_runner(ssh_conn_object, commands):
  16. output = ssh_conn_object.send_command(commands)
  17.  
  18. '''
  19. # parse output
  20. '''
  21.  
  22. return output
  23.  
  24.  
  25. def metric_2_cmd_runner(ssh_conn_object, commands):
  26. output = ssh_conn_object.send_command(commands)
  27.  
  28. '''
  29. # parse output
  30. '''
  31.  
  32. return output
  33.  
  34.  
  35. def get_first_metric(ipaddress, commands):
  36. '''
  37. # Create SSH session here to pass to executor as object
  38. '''
  39.  
  40. executor_data = {}
  41.  
  42. with ThreadPoolExecutor(max_workers=100) as EXECUTOR:
  43. ssh_exec = [EXECUTOR.submit(metric_1_cmd_runner,
  44. desc,
  45. desc_args) for
  46. desc, desc_args in executor_data.items()]
  47.  
  48.  
  49. def get_second_metric(ipaddress, commands):
  50. '''
  51. # Create SSH session here to pass to executor as object
  52. '''
  53.  
  54. executor_data = {}
  55.  
  56. with ThreadPoolExecutor(max_workers=100) as EXECUTOR:
  57. ssh_exec = [EXECUTOR.submit(metric_2_cmd_runner,
  58. desc,
  59. desc_args) for
  60. desc, desc_args in executor_data.items()]
  61.  
  62.  
  63. def worker(ipaddress, command_data, MAINPROC_QUEUE):
  64.  
  65. first_metric_show_commands = []
  66.  
  67. second_metric_show_commands = []
  68.  
  69. METRIC_POOL = Pool(processes=2)
  70.  
  71. first_metric_restults = METRIC_POOL.apply_async(
  72. get_first_metric,
  73. args=(ipaddress,
  74. first_metric_show_commands)
  75. )
  76. second_metric_results = METRIC_POOL.apply_async(
  77. get_second_metric,
  78. args=(ipaddress,
  79. second_metric_show_commands)
  80. )
  81.  
  82. mt1_results = first_metric_restults.get()
  83. mt2_results = second_metric_results.get()
  84.  
  85. METRIC_POOL.close()
  86. METRIC_POOL.join()
  87. MAINPROC_QUEUE.put([mt1_results, mt2_results])
  88.  
  89.  
  90. def main():
  91. MAINPROC_QUEUE = multiprocessing.Queue()
  92.  
  93. ''' open data file '''
  94. ip_node_association = {}
  95.  
  96. main_proc = []
  97. QUEUE_RESULTS = []
  98.  
  99. for ipaddress, tree in ip_node_association.items():
  100. CMTS_MAIN_PROCESSES = multiprocessing.Process(
  101. target=worker,
  102. name=str(ipaddress),
  103. args=(ipaddress, tree, MAINPROC_QUEUE)
  104. )
  105. CMTS_MAIN_PROCESSES.daemon = False
  106. CMTS_MAIN_PROCESSES.start()
  107. main_proc.append(CMTS_MAIN_PROCESSES)
  108. for p in main_proc:
  109. QUEUE_RESULTS.append(MAINPROC_QUEUE.get())
  110. CMTS_MAIN_PROCESSES.join()
  111.  
  112. for i in main_proc:
  113. print(i.name, i.exitcode)
  114.  
  115. for i in QUEUE_RESULTS:
  116. print(json.dumps(i, indent=2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement