当我从另一个文件导入一个函数并进行部署时,它不起作用。 它给我:错误 500 内部服务器错误。 python 要求工作正常,因为 hello 函数没有给我错误。但是 getPrediction 效果不佳
在离线模式下一切正常。
我的 handler.js
try:
import unzip_requirements
except ImportError:
pass
import json
import numpy
import scipy
from predictions.make_prediction import make_prediction
def hello(event, context):
response = {"statusCode": 200, "body": {mod.__name__: mod.__version__ for mod in (numpy, scipy)}}
return response
def getPrediction(event, context):
hola = make_prediction([1,2,3,4,5,6,7])
response = {"statusCode": 200, "body": json.dumps(hola.tolist())}
return response
我的 serverless.yml
service: enno-searcher
frameworkVersion: '3'
provider:
name: aws
runtime: python3.7
timeout: 29
plugins:
- serverless-offline
- serverless-python-requirements
package:
patterns:
- '!node_modules/**'
- '!Lib/**'
- '!README.md'
- '!build/**'
- '!__pycache__/**'
- '!Include/**'
custom:
pythonRequirements:
dockerizePip: true
zip: true
functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /
method: get
getPrediction:
handler: handler.getPrediction
events:
- httpApi:
path: /get-prediction
method: get
我试过改变我导入外部函数的方式,但它总是给我同样的错误。也许我做错了
我也试过不从其他文件导入函数。我试过将代码从 make_prediction 带到 getPrediction,但它一直给我一个错误。
我已经设法用无服务器框架解决了我的问题。经过多次测试,这就是我的文件“handler.py”和我的“serverless.yml”的保留方式
无服务器.yml
service: enno-searcher
frameworkVersion: '3'
provider:
name: aws
runtime: python3.7
timeout: 29
httpApi:
cors: true
package:
patterns:
- '!node_modules/**'
- '!Lib/**'
- '!README.md'
- '!build/**'
- '!__pycache__/**'
- '!Include/**'
functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /
method: get
test:
handler: handler.test
events:
- httpApi:
path: /test
method: get
getprediction:
handler: handler.getPrediction
events:
- httpApi:
path: /get-prediction
method: get
plugins:
- serverless-offline
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: non-linux
zip: true
处理程序.py
try:
import unzip_requirements
except ImportError:
pass
import json
def hello(event, context):
response = {
"statusCode": 200,
'headers': {
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Credentials": True
},
"body": 'Searcher Backend v0.0.1'}
return response
def test(event, context):
from predictions.make_prediction import make_prediction
hola = make_prediction([1,2,3,4,5,6,7])
response = {
"statusCode": 200,
'headers': {
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Credentials": True
},
"body": json.dumps(
hola.tolist()
)}
return response
def getPrediction(event, context):
try:
from predictions.make_prediction import make_prediction
inputPrediction = event['queryStringParameters']['prediction']
prediction = make_prediction(eval(inputPrediction))
response = {
"statusCode": 200,
'headers': {
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Credentials": True
},
"body": json.dumps(
prediction.tolist()
)}
return response
except KeyError as e:
error_message = f'Faltan los parametros requeridos: {e}'
response = {
'statusCode': 400,
'headers': {
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Credentials": True
},
'body': json.dumps({'error': error_message})
}
return response
except Exception as e:
# Captura cualquier otra excepción y devuelve un mensaje de error genérico
error_message = f'Se produjo un error al procesar la solicitud: {e}'
response = {
'statusCode': 500,
'headers': {
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Credentials": True
},
'body': json.dumps({'error': error_message})
}
return response
这样,我已经成功地从其他文件导入功能了。通过在每个函数中导入它,我设法控制了错误。我还添加了 try 和 except 来进一步处理错误。我还在 API 中添加了 cors。
此外,我还添加了更多函数(我没有放在这里),其结构与“getPrediction”函数相同。他们对我来说都很完美。