我正在尝试在我想部署在Heroku上的Flask应用程序中设置model.predict函数。该预测在本地可以很好地运行,但是在已部署的应用程序上调用该函数时,它将遇到30秒超时错误。
我找到了一些提到设置工人的线程,这是我尝试做的,但是我认为我设置错误的工作。我已经建立了在Heroku Redis的,并且它打它,但我不能找到合适的文档,以找出原因作业没有完成。它使进入一个故障状态。
/worker.py
import redis
from rq import Worker, Queue, Connection
listen = ['high', 'default', 'low']
redis_url = os.getenv('REDIS_URL', 'redis://localhost:6379')
conn = redis.from_url(redis_url)
if __name__ == '__main__':
with Connection(conn):
worker = Worker(map(Queue, listen))
print(f'The worker is: {worker}')
worker.work()
/utils.py
def prepare_img(img):
global model
global graph
print(f'img: {img}')
data = {}
random_hex = secrets.token_hex(8)
print(f'random_hex: {random_hex}')
_, f_ext = os.path.splitext(img.filename)
print(f'f_ext: {f_ext}')
picture_fname = f'{random_hex}{f_ext}'
print(f'picture_fname: {picture_fname}')
picture_path = os.path.join(current_app.root_path, 'static/uploads', picture_fname)
print(f'picture_path: {picture_path}')
output_size = (299, 299)
i = Image.open(img)
i.thumbnail(output_size)
i.save(picture_path)
im = keras.preprocessing.image.load_img(picture_path, target_size=output_size, grayscale=False)
print(f'im: {im}')
prepared_img = img_to_array(im)
prepared_img = np.expand_dims(prepared_img, axis=0)
prepared_img = preprocess_input(prepared_img)
print(f'prepared_img: {prepared_img}')
print(f'graph: {graph}')
with graph.as_default():
preds = model.predict(prepared_img, verbose=1)
print(f'preds: {preds}')
results = decode_predictions(preds)
print(f'results: {results}')
data['predictions'] = []
for (imagenetID, label, prob) in results[0]:
r = {"label": label, "probability": round(100*float(prob),2)}
data['predictions'].append(r)
print(f'data: {data}')
return data, picture_fname
这是我在本地运行时的输出,预测最多需要2秒钟。
但是,当Heroku应用程序运行预测时,页面会通过下面的print语句执行大多数步骤,但是会陷入model.predict步骤,并随后超时。有没有人有一个很好的例子说明如何使用工人的工作来解决这个问题?
2019-01-31T05:04:21.710359+00:00 app[web.1]: <website.analytics.forms.ClassificationForm object at 0x7f09ab3cf710>
2019-01-31T05:04:21.710407+00:00 app[web.1]: img: <FileStorage: '36686706_227230454585436_339126893955514368_n.jpg' ('image/jpeg')>
2019-01-31T05:04:21.710491+00:00 app[web.1]: random_hex: 9ab07f8c1c3521af
2019-01-31T05:04:21.710528+00:00 app[web.1]: f_ext: .jpg
2019-01-31T05:04:21.710534+00:00 app[web.1]: picture_fname: 9ab07f8c1c3521af.jpg
2019-01-31T05:04:21.710588+00:00 app[web.1]: picture_path: /app/website/static/uploads/9ab07f8c1c3521af.jpg
2019-01-31T05:04:21.759655+00:00 app[web.1]: im: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=299x299 at 0x7F09AB48B5C0>
2019-01-31T05:04:21.768021+00:00 app[web.1]: prepared_img: [[[[-0.94509804 -0.90588236 -0.8745098 ]
2019-01-31T05:04:21.768024+00:00 app[web.1]: [-0.9529412 -0.8901961 -0.8666667 ]
2019-01-31T05:04:21.768026+00:00 app[web.1]: [-0.94509804 -0.88235295 -0.85882354]
2019-01-31T05:04:21.768028+00:00 app[web.1]: ...
2019-01-31T05:04:21.768029+00:00 app[web.1]: [-0.64705884 -0.49019605 -0.56078434]
2019-01-31T05:04:21.768031+00:00 app[web.1]: [-0.654902 -0.4980392 -0.5686275 ]
2019-01-31T05:04:21.768033+00:00 app[web.1]: [-0.6627451 -0.5058824 -0.5764706 ]]
2019-01-31T05:04:21.768034+00:00 app[web.1]:
2019-01-31T05:04:21.768036+00:00 app[web.1]: [[-0.9529412 -0.8901961 -0.8666667 ]
2019-01-31T05:04:21.768037+00:00 app[web.1]: [-0.9529412 -0.8901961 -0.8666667 ]
2019-01-31T05:04:21.768039+00:00 app[web.1]: [-0.9529412 -0.8745098 -0.85882354]
2019-01-31T05:04:21.768041+00:00 app[web.1]: ...
2019-01-31T05:04:21.768043+00:00 app[web.1]: [-0.6784314 -0.52156866 -0.5921569 ]
2019-01-31T05:04:21.768045+00:00 app[web.1]: [-0.67058825 -0.5137255 -0.58431375]
2019-01-31T05:04:21.768046+00:00 app[web.1]: [-0.6784314 -0.52156866 -0.5921569 ]]
2019-01-31T05:04:21.768048+00:00 app[web.1]:
2019-01-31T05:04:21.768049+00:00 app[web.1]: [[-0.9372549 -0.8745098 -0.8509804 ]
2019-01-31T05:04:21.768051+00:00 app[web.1]: [-0.9529412 -0.8745098 -0.85882354]
2019-01-31T05:04:21.768052+00:00 app[web.1]: [-0.9529412 -0.8745098 -0.85882354]
2019-01-31T05:04:21.768054+00:00 app[web.1]: ...
2019-01-31T05:04:21.768056+00:00 app[web.1]: [-0.654902 -0.4980392 -0.5686275 ]
2019-01-31T05:04:21.768057+00:00 app[web.1]: [-0.6392157 -0.4823529 -0.5529412 ]
2019-01-31T05:04:21.768059+00:00 app[web.1]: [-0.62352943 -0.46666664 -0.5372549 ]]
2019-01-31T05:04:21.768060+00:00 app[web.1]:
2019-01-31T05:04:21.768062+00:00 app[web.1]: ...
2019-01-31T05:04:21.768063+00:00 app[web.1]:
2019-01-31T05:04:21.768082+00:00 app[web.1]: [[-0.7411765 -0.69411767 -0.7882353 ]
2019-01-31T05:04:21.768088+00:00 app[web.1]: [-0.70980394 -0.6627451 -0.75686276]
2019-01-31T05:04:21.768089+00:00 app[web.1]: [-0.6862745 -0.6392157 -0.73333335]
2019-01-31T05:04:21.768091+00:00 app[web.1]: ...
2019-01-31T05:04:21.768092+00:00 app[web.1]: [-0.19215685 -0.19215685 -0.19215685]
2019-01-31T05:04:21.768094+00:00 app[web.1]: [-0.2235294 -0.2235294 -0.2235294 ]
2019-01-31T05:04:21.768096+00:00 app[web.1]: [-0.27058822 -0.25490195 -0.26274508]]
2019-01-31T05:04:21.768097+00:00 app[web.1]:
2019-01-31T05:04:21.768099+00:00 app[web.1]: [[-0.75686276 -0.70980394 -0.8039216 ]
2019-01-31T05:04:21.768100+00:00 app[web.1]: [-0.7254902 -0.6784314 -0.77254903]
2019-01-31T05:04:21.768102+00:00 app[web.1]: [-0.6862745 -0.6392157 -0.73333335]
2019-01-31T05:04:21.768103+00:00 app[web.1]: ...
2019-01-31T05:04:21.768105+00:00 app[web.1]: [-0.16862744 -0.16862744 -0.16862744]
2019-01-31T05:04:21.768106+00:00 app[web.1]: [-0.19999999 -0.19999999 -0.19999999]
2019-01-31T05:04:21.768108+00:00 app[web.1]: [-0.24705881 -0.23137254 -0.23921567]]
2019-01-31T05:04:21.768109+00:00 app[web.1]:
2019-01-31T05:04:21.768110+00:00 app[web.1]: [[-0.75686276 -0.73333335 -0.81960785]
2019-01-31T05:04:21.768112+00:00 app[web.1]: [-0.7254902 -0.7019608 -0.7882353 ]
2019-01-31T05:04:21.768114+00:00 app[web.1]: [-0.6784314 -0.654902 -0.7411765 ]
2019-01-31T05:04:21.768115+00:00 app[web.1]: ...
2019-01-31T05:04:21.768117+00:00 app[web.1]: [-0.1372549 -0.1372549 -0.15294117]
2019-01-31T05:04:21.768118+00:00 app[web.1]: [-0.16862744 -0.16862744 -0.18431371]
2019-01-31T05:04:21.768120+00:00 app[web.1]: [-0.21568626 -0.19999999 -0.2235294 ]]]]
2019-01-31T05:04:21.768127+00:00 app[web.1]: graph: <tensorflow.python.framework.ops.Graph object at 0x7f0964bf4828>
2019-01-31T05:04:51.706165+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/analytics" host=evanbeese.herokuapp.com request_id=bcbe637c-a2e5-4e82-a75b-01dee4a95c85 fwd="174.53.250.66" dyno=web.1 connect=1ms service=30180ms status=503 bytes=0 protocol=http
/utils.py
def get_model_response(img):
print(f'get_model_response img: {img}')
result = q.enqueue_call(func=prepare_img, args=([img]), result_ttl=600)
while result.result is None:
pass
return result.result
/routes.py
picture_file, picture_path = get_model_response(form.picture.data)
如果有帮助,该应用程序当前已部署在http://evanbeese.herokuapp.com上。