python中的多线程
我正在尝试使用python创建EC2实例的AMI。我需要实现多线程,以便AMI创建可以并行运行。但下面的代码不起作用。它一次创建一个AMI 模块1python中的多线程,python,amazon-web-services,amazon-ami,Python,Amazon Web Services,Amazon Ami,我正在尝试使用python创建EC2实例的AMI。我需要实现多线程,以便AMI创建可以并行运行。但下面的代码不起作用。它一次创建一个AMI 模块1 from Libpy import Libpy import boto.ec2 import sys if __name__=='__main__': a = B() Libpy().multithreading(ec2list1,a.create_amibkp(ec2listname,ec2list1)) 模块2 import thre
from Libpy import Libpy
import boto.ec2
import sys
if __name__=='__main__':
a = B()
Libpy().multithreading(ec2list1,a.create_amibkp(ec2listname,ec2list1))
模块2
import threading
import time
class Libpy:
def multithreading(l, function):
threads = []
for z,v in enumerate(l):
dummy = z
t = threading.Thread(target=function, args=(v,))
threads.append(t)
t.start()
我无法重现您的示例,但我创建了一个虚拟版本:
import sys
import threading
import time
import random
random.seed(1)
class Libpy:
def multithreading(self, lst, function):
threads = []
for v in lst:
t = threading.Thread(target=function, args=(v,))
t.start()
threads.append(t)
for t in threads:
t.join()
def ami_creation(v):
t = random.random() * 4
print("creating ami {0}, sleeping {1}".format(v, t))
time.sleep(t)
print("ami {0} created ok".format(v))
ec2list1 = ["ec2-small", "ec2-medium", "ec2-large"]
Libpy().multithreading(ec2list1, ami_creation)
print("All boxes have been created...")
在上面的示例中,主线程将等待所有框被创建。在我的回答中,我已经向您展示了如何并行生成多个线程(无延迟触发),并且只有当所有线程完成其业务后,主线程才会继续执行,这是因为threads:t.join()中t的行
。我建议阅读,这将比此处的任何简短评论更清楚。嗨,我在尝试执行AMI备份时遇到以下错误BotoServerError:BotoServerError:503服务不可用requestLimitExceed
requestLimitExceed.30694d63-d23e-4f28-a892-ab9d958ca0fa请帮助