AWS Lambda多线程和boto3

时间:2017-11-10 02:36:00

标签: python aws-lambda boto3

我正在编写AWS Lambda函数,该函数从S3存储桶中删除每个lambda函数调用100,000个对象。我试图看看我是否可以在后台线程上创建并运行删除。我有以下代码。

import boto3
import boto3.session
from threading import Thread

http_response = []
MAX = 999
threads = []

class myThread(Thread):
  def __init__(self, objects_to_delete, bucket_name):
    Thread.__init__(self)

    self.objects_to_delete = objects_to_delete
    self.bucket_name = bucket_name

  def run(self):
    session = boto3.session.Session().client('s3')
    s3 = session.client('s3')

    #### 
     COMES HERE AND PRINTS THE NAME OF THE BUCKET.
    ####
    print(self.bucket_name)

    response = s3.delete_objects(Bucket=bucket_name, Delete={'Objects': objects_to_delete[0:MAX] })

    #### 
      THIS IS NOT GETTING PRINTED. MEANING, delete_object IS BREAKING/NOT EXECUTING
    ####
    print(response)


def handler(event, context):

  keys = event['keys']
  bucket_name = event["bucket"]

   if (len(keys) == 0 or len(bucket_name) == 0):
    return {
        "message": http_response
    }

try:
        t = myThread(objects_to_delete[0:MAX], bucket_name)
        t.start()
        threads.append(t)

except:
    print("Something Went wrong!!! " + str(objects_to_delete))


del keys[0:MAX]

 for i in range(len(threads)):
     threads[i].start()


handler({'keys': keys, 'bucket': bucket_name}, context)

我在这里做错了吗?似乎线程正在开始,但是它没有制作" delete_objects"呼叫。它甚至没有返回任何错误消息来了解错误。有什么想法或想法吗?

还有一件事,当我在我的电脑上本地运行此功能时,它运行正常,没有任何问题。

1 个答案:

答案 0 :(得分:2)

在启动一个线程后结果,你应该加入它们,因为一旦进程退出,线程也会死掉。所以我做了以下

import boto3
from threading import Thread

MAX = 999
threads = []

class myThread(Thread):
    def __init__(self, bucket_name, objects):
        Thread.__init__(self)
        self.bucket_name = bucket_name
        self.objects = objects

def run(self):
    s3 = boto3.client('s3', region_name="us-east-1")
    response = s3.delete_objects(Bucket=self.bucket_name, Delete={'Objects':self.objects})
    print(response)


 def handler(event, context):

   keys = event["keys"]
   bucket_name = event["bucket"]

   objects_to_delete = [1...100,000]

   while (len(objects_to_delete) != 0):
      t = myThread(bucket_name, objects_to_delete[0:MAX])
      threads.append(t)
      del objects_to_delete[0:MAX]


    for thread in threads:
       thread.start()

    for thread in threads:
       thread.join()

    return {
       "message": "Success Message."
    }