我在预处理某些数据时遇到此有效错误:
9:46:56.323 PM default_model Function execution took 6008 ms, finished with status: 'crash'
9:46:56.322 PM default_model Traceback (most recent call last):
File "/user_code/main.py", line 31, in default_model
train, endog, exog, _, _, rawDf = preprocess(ledger, apps)
File "/user_code/Wrangling.py", line 73, in preprocess
raise InsufficientTimespanError(args=(appDf, locDf))
发生在这里:
async def default_model(request):
request_json = request.get_json()
if not request_json:
return '{"error": "empty body." }'
if 'transaction_id' in request_json:
transaction_id = request_json['transaction_id']
apps = [] # array of apps whose predictions we want, or uempty for all
if 'apps' in request_json:
apps = request_json['apps']
modelUrl = None
if 'files' in request_json:
try:
files = request_json['files']
modelUrl = getModelFromFiles(files)
except:
return package(transaction_id, error="no model to execute")
else:
return package(transaction_id, error="no model to execute")
if 'ledger' in request_json:
ledger = request_json['ledger']
try:
train, endog, exog, _, _, rawDf = preprocess(ledger, apps)
# ...
except InsufficientTimespanError as err:
return package(transaction_id, error=err.message, appDf=err.args[0], locDf=err.args[1])
预处理正确地抛出了我的自定义错误:
def preprocess(ledger, apps=[]):
"""
convert ledger from the server, which comes in as an array of csv entries.
normalize/resample timeseries, returning dataframes
"""
appDf, locDf = splitLedger(ledger)
if len(appDf) < 3 or len(locDf) < 3:
raise InsufficientDataError(args=(appDf, locDf))
endog = appDf['app_id'].unique().tolist()
exog = locDf['location_id'].unique().tolist()
rawDf = normalize(appDf, locDf)
trainDf = cutoff(rawDf.copy(), apps)
rawDf = cutoff(rawDf.copy(), apps, trim=False)
# TODO - uncomment when on realish data
if len(trainDf) < 2 * WEEKS:
raise InsufficientTimespanError(args=(appDf, locDf))
问题是,它位于try``except
块中,正是因为我想捕获错误并返回带有错误的有效负载,而不是因为500个错误而崩溃。但它在我的自定义错误崩溃,在尝试块,无论如何。就在电话那头preprocess
。你知道吗
对于我来说,这一定是一个失败,无法符合正确的python代码。但我不确定我做错了什么。环境是python3.7
这就是错误的定义,在争吵.py地址:
class WranglingError(Exception):
"""Base class for other exceptions"""
pass
class InsufficientDataError(WranglingError):
"""insufficient data to make a prediction"""
def __init__(self, message='insufficient data to make a prediction', args=None):
super().__init__(message)
self.message = message
self.args = args
class InsufficientTimespanError(WranglingError):
"""insufficient timespan to make a prediction"""
def __init__(self, message='insufficient timespan to make a prediction', args=None):
super().__init__(message)
self.message = message
self.args = args
下面是如何主.py声明(导入)它:
from Wrangling import preprocess, InsufficientDataError, InsufficientTimespanError, DataNotNormal, InappropriateValueToPredict
错误中的行号与代码中的行号匹配吗?如果不是,那么在添加try…except之前,是否有可能从某个版本的代码中看到错误?你知道吗
您的
preprocess
函数被声明为async
。这意味着它中的代码实际上不是在调用preprocess
的地方运行的,而是在它最终被await
编辑或传递到主循环(如asyncio.run
)的时候运行的。因为运行异常的位置不再位于default_model
中的try块中,所以不会捕获异常。你知道吗您可以通过以下几种方式解决此问题:
preprocess
不异步default_model
也异步,并且await
在preprocess
上。你知道吗相关问题 更多 >
编程相关推荐