提前谢谢你的帮助…我卡住了!我正在尝试编写一个脚本,该脚本将启动多个进程,每个进程都执行以下操作:
1启动AWS EC2实例
2在该实例上执行脚本(AMI上的脚本)
三。脚本完成后终止实例
使用单独的vm确实是必要的,这对于另一种本地并行类型来说不是一个好的例子。在
下面的脚本可以工作,但池工作人员停止响应。我从目标名单中得到了一部分,泳池工人什么也没做。他们所处的进程仍在运行,VM在AWS上仍然是活动的,但是什么也没有发生,事情似乎挂起了。在
另外,当我使用调试器/IDE和一个池工作器逐步检查它时,它可以正常工作。当我在mp池中运行它时,它们在~2次迭代后挂起。在import postgresql
import boto3
import paramiko
import time
import os
from multiprocessing import Pool
def spin_up(target_co):
#What would happen with paramiko import here?
ec2 = boto3.resource('ec2', region_name="us-west-2")
instances = ec2.create_instances(ImageId='AMI-HERE',
MinCount=1,
MaxCount=1,
KeyName="SECRET_KEY",
InstanceType="t2.micro",
Placement={
'AvailabilityZone': 'us-west-2c'
},
SecurityGroups=[
'Internal'
]
)
i = instances[0]
print('WAITING FOR INSTANCE AVAILABILITY....')
i.wait_until_running()
print('OK.')
i.load()
k = paramiko.RSAKey.from_private_key_file('KEY_FILE_PATH.pem')
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
success_flag = False
attempt_counter = 0
while success_flag == False and attempt_counter < 20:
try:
c.connect(hostname=i.public_dns_name, username="ubuntu", pkey=k)
success_flag = True
except:
print('SSH Error.....Retrying.')
attempt_counter += 1
time.sleep(5)
cmd = "python3 PATH_TO_EXECUTABLE_ON_AMI.py --target {} > LOG_FILE_ON_VM".format(target_co)
transport = c.get_transport()
channel = transport.open_session()
channel.exec_command(cmd)
while(channel.exit_status < 0):
print(str(os.getpid()) + ' sleeping...')
time.sleep(60)
print('Terminating Instance....')
i.terminate()
print('Exiting....')
if __name__ == '__main__':
target_cos = ['539',
'542',
'528',
'48',
'536',
'26',
'7',
'20572',
'10',
'20',
'101',
'10023']
# PARALLEL
with Pool(processes=2) as pool:
pool.map_async(spin_up, iter(target_cos))
#are these actually required?
pool.close()
pool.join()