import psutil,time
import subprocess
import multiprocessing
def execute_command(command):
# subprocess.call(command, shell=True)
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def monitor_memory():
mem_usage = psutil.virtual_memory().percent
if mem_usage > 70:
return False
else:
return True
if __name__ == "__main__":
commands = [
] # 替换成实际的命令列表
# 创建一个最大数为8的进程池
with multiprocessing.Pool(8) as pool:
for command in commands:
mm = monitor_memory()
while True:
time.sleep(10)
if(mm):
break
if mm:
print(command)
pool.apply_async(execute_command, (command,))
# 关闭进程池,不再接受新的任务
pool.close()
# 等待所有进程执行完毕
pool.join()