【发布时间】:2021-06-15 20:05:44
【问题描述】:
我有很多 AWS EC2 实例,我需要同时从它们执行一个 python 脚本。
我一直在尝试从我的电脑通过 ssh 发送所需的命令来执行脚本。为此,我创建了另一个 python 脚本,它打开一个 cmd 终端,然后执行一些命令(我需要在每个实例上执行 python 脚本的命令)。因为我需要同时打开所有这些 cmd 终端,所以我使用了 ThreatPoolExecutor,它(具有我的 PC 特性)允许我并行运行 60 次。这是代码:
import os
from concurrent.futures import ThreadPoolExecutor
ipAddressesList=list(open("hosts.txt").read().splitlines())
def functionMain(threadID):
os.system(r'start cmd ssh -o StrictHostKeyChecking=no -i mysshkey.pem ec2-user@'+ipAddressesList[threadID]+' "cd scripts && python3.7 script.py"')
functionMainList =list(range(0,len(ipAddressesList)))
with ThreadPoolExecutor() as executor:
results = executor.map(functionMain, functionMainList)
问题在于执行script.py 的命令会阻塞终端直到进程结束,因此functionMain 一直在等待结果。我想找到在发送命令python3.7 script.py 后函数结束但脚本继续在实例中执行的方式。所以池执行器可以继续线程。
【问题讨论】:
标签: python-3.x amazon-ec2 ssh cmd python-multiprocessing