로부터 AWS 람다 함수 작업을위한 모범 사례 :
실행 컨텍스트 재사용을 활용하여 함수의 성능을 개선하십시오. 함수 핸들러 외부에서 SDK 클라이언트 및 데이터베이스 연결 초기화, [...]
함수가 호출 될 때마다 데이터베이스 핸들이 초기화되고 닫히는 람다 함수를 개선하기 위해이 원칙을 구현하고 싶습니다. 다음 예를 살펴보십시오.
def lambda_handler(event, context):
# Open a connection to the database
db_handle = connect_database()
# Do something with the database
result = perform_actions(db_handle)
# Clean up, close the connection
db_handle.close()
# Return the result
return result
AWS 설명서에 대한 이해를 바탕으로 코드는 다음과 같이 최적화되어야합니다.
# Initialize the database connection outside the handler
db_handle = conn_database()
def lambda_handler(event, context):
# Do something with the database and return the result
return perform_actions(db_handle)
이로 인해 db_handle.close()
메서드가 호출되지 않아 잠재적으로 연결이 누출됩니다.
Python과 함께 AWS Lambda를 사용할 때 이러한 리소스의 정리를 어떻게 처리해야합니까?
많은 사람들이 당신과 같은 것을 찾고 있습니다. 지금은 불가능하다고 생각합니다. 그러나 우리는 데이터베이스 쪽에서 문제를 처리 할 수 있습니다.
이것 좀 봐
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다