# !/usr/bin/env python3
# -*- coding:utf-8 -*-
# __author__ =import subprocess, json, os
import threading, queuekubectl_cmd = '/usr/local/bin/kubectl'
node_roles = 'worker'
max_conn = 5def exec_kubectl(node_roles):kubectlcommand = f"sudo {kubectl_cmd} get node | grep {node_roles}"# 使用awk和sed把shell命令执行结果转换为json格式awkcommand = "awk '{ printf \"\\\"%s\\\", \", $1 } END { print \"\" }' | sed 's/, $//; s/^/[/; s/$/]/'"command = f"{kubectlcommand}|{awkcommand}"result = subprocess.run(command, shell=True, capture_output=True, text=True)return json.loads(result.stdout)def exec_nci(dev_q):while not dev_q.empty():res = dev_q.get()os.system(f"echo {res}")if __name__ == '__main__':devs_q = queue.Queue()ts = []for i in exec_kubectl(node_roles):devs_q.put(i)for i in range(max_conn):t = threading.Thread(target=exec_nci, args=(devs_q,))t.start()ts.append(t)for t in ts:t.join()
2、使用ThreadPoolExecutor模块实现
# !/usr/bin/env python3
# -*- coding:utf-8 -*-
# __author__ =from concurrent.futures import ThreadPoolExecutor, as_completed
import time # 计算平方的函数
def square(x): time.sleep(1) # 模拟延迟 return x * x # 回调函数
def callback(future): result = future.result() # 获取结果print(f'Result: {result}') def main(): numbers = [1, 2, 3, 4, 5]# 创建一个线程池 with ThreadPoolExecutor(max_workers=4) as executor:# 提交任务并设置回调函数futures = {executor.submit(square, num): num for num in numbers}# 使用as_completed可以确保无论任务提交的顺序如何,回调函数都会在每个任务完成后立即执行for future in as_completed(futures):future.add_done_callback(callback)if __name__ == "__main__": main()
3、使用multiprocessing模块实现
# !/usr/bin/env python3
# -*- coding:utf-8 -*-
# __author__ =import multiprocessing
import time # 计算平方的函数
def square(x): time.sleep(1) # 模拟延迟 return x * x # 回调函数
def callback(result): print(f'Result: {result}') def main(): # 创建一个进程池 with multiprocessing.Pool(processes=4) as pool: # 可以根据系统的CPU核心数设置进程数量 numbers = [1, 2, 3, 4, 5] # 使用 apply_async 方法提交任务for number in numbers: pool.apply_async(square, args=(number,), callback=callback) # 关闭池并等待所有任务完成pool.close() pool.join() # 等待所有进程结束if __name__ == "__main__": main()