'How to perform 2 operations in parallel?

I have a problem and I can't figure it out. I have the following code that must save the data that is sent to him by an http request, first on the aws bucket and then by a request on the db. Except that 1 of the 2 operations does not work. Is there a way to do the 2 operations in parallel?

@staticmethod
def ingest_data(video, metadata, video_dst_path, metadata_dst_path, detected):
    """
    Method that insert a video and a json metadata file into AWS-S3
    :param video: video file forwarded by the ingestion endpoint
    :param video_dst_path: path to save the video on AWS-S3
    :param metadata: json file forwarded by the ingestion endpoint
    :param metadata_dst_path: path to save the json on AWS-S3
    :return: response
        :type {"message": ""}, code status
    """
    try:
        # This part is for EAV exit plan 
        files = {'video': video, 'metadata': metadata, 'detected': detected}
        # threading.Thread(target=forward, args=(files,)).start()
        ###########################################################
        # resp = requests.post('http://192.168.1.175:8000/forward', files=files)
        print(files, flush=True)
        print(metadata,flush=True)
        sleep(15)
        s3 = boto3.resource(
            service_name='s3',
            aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
            aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY")
        )

            
        object = s3.Object(os.getenv("BUCKET_NAME"), video_dst_path) 
        object.put(Bucket=os.getenv("BUCKET_NAME"), Key=video_dst_path, Body=video)

        print('Dumping and creating object {} in S3 {} bucket'.format(video_dst_path, os.getenv("BUCKET_NAME")),flush=True)

        object = s3.Object(os.getenv("BUCKET_NAME"), metadata_dst_path)
        object.put(Bucket=os.getenv("BUCKET_NAME"), Key=metadata_dst_path, Body=metadata)

        print('Dumping and creating object {} in S3 {} bucket'.format(metadata_dst_path, os.getenv("BUCKET_NAME")),flush=True)

        print(files, flush=True)
        print(metadata,flush=True)

        #This is the request for saving the data on the db
        requests.post('http://192.168.1.175:8000/forward', files=files)
        
        

    except ClientError as ex:
        if ex.response['Error']['Code'] == 'NoSuchKey':
            return {'message': 'Server has encountered some error'}, 500
        else:
            return ex.response['Error']['Code']

    return {"message": "OK"}, 200

I entered the threads as advised but I get this error:enter image description here

the modified code is the following:

staticmethod
def ingest_data(video, metadata, video_dst_path, metadata_dst_path, detected):

    files = {'video': video, 'metadata': metadata, 'detected': detected}
    t1 = threading.Thread(target=forward, args=(files))
    """
    Method that insert a video and a json metadata file into AWS-S3
    :param video: video file forwarded by the ingestion endpoint
    :param video_dst_path: path to save the video on AWS-S3
    :param metadata: json file forwarded by the ingestion endpoint
    :param metadata_dst_path: path to save the json on AWS-S3
    :return: response
        :type {"message": ""}, code status
    """
    try:
        t1.start()
        s3 = boto3.resource(
            service_name='s3',
            aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
            aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY")
        )

            
        object = s3.Object(os.getenv("BUCKET_NAME"), video_dst_path) 
        object.put(Bucket=os.getenv("BUCKET_NAME"), Key=video_dst_path, Body=video)

        print('Dumping and creating object {} in S3 {} bucket'.format(video_dst_path, os.getenv("BUCKET_NAME")),flush=True)

        object = s3.Object(os.getenv("BUCKET_NAME"), metadata_dst_path)
        object.put(Bucket=os.getenv("BUCKET_NAME"), Key=metadata_dst_path, Body=metadata)

        print('Dumping and creating object {} in S3 {} bucket'.format(metadata_dst_path, os.getenv("BUCKET_NAME")),flush=True)

        print(files, flush=True)
        print(metadata,flush=True)

        #This is the request for saving the data on the db
        # requests.post('http://192.168.1.175:8000/forward', files=files)
        
        

    except ClientError as ex:
        if ex.response['Error']['Code'] == 'NoSuchKey':
            return {'message': 'Server has encountered some error'}, 500
        else:
            return ex.response['Error']['Code']

    return {"message": "OK"}, 200

new method that made the post

def forward(files):
requests.post('http://192.168.1.175:8000/forward', files=files)
print("Sent to Django",flush=True)
print(files, flush=True)

I thank in advance anyone who can help me



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source