import os import dotenv import boto3 import botocore import json from concurrent import futures from functools import wraps import time dotenv.load('.env') from models import asset_final LAMBDA = os.environ.get('LAMBDA') _DEFAULT_POOL = futures.ThreadPoolExecutor() def threadpool(f, executor=None): @wraps(f) def wrap(*args, **kwargs): return (executor or _DEFAULT_POOL).submit(f, *args, **kwargs) return wrap def check_file_exists(file): s3 = boto3.resource('s3') try: s3.Object(file['bucket'], file['key']).load() except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == "404": print('File does not exist') return False else: print(e) raise return True def get_file_metadata(file): client = boto3.client('lambda') try: result = client.invoke( FunctionName=LAMBDA, InvocationType='RequestResponse', LogType='None', Payload=json.dumps(file) ) metadata = json.loads(result['Payload'].read().decode("utf-8")) print(metadata) except botocore.exceptions.ClientError as e: print(e) @threadpool def process_file(file): if check_file_exists(file): get_file_metadata(file) start_time = time.time() test_files = asset_final.get_batch(5) for test_file in test_files: process_file(test_file) _DEFAULT_POOL.shutdown(wait=True) end_time = time.time() d = end_time - start_time print(d)