Как выполнить 2 операции параллельно?
У меня проблема и я не могу ее решить. У меня есть следующий код, который должен сохранить данные, отправленные ему http запросом, сначала на aws bucket, а потом запросом на db. Только вот 1 из 2 операций не работает. Есть ли способ сделать эти 2 операции параллельно?
@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