Introduction
Recently I’ve been working on a project using flask-restful API to deploy AI models. I am completely new to Flask (and multiprocessing programming), so here is a blog recording the issues I met.
Issues
Flask app initiated twice
Reason: when running flask app, if the debug mode is on, then the app would be initiated twice.
Reference (https://blog.csdn.net/sun_dragon/article/details/51861139):
python flask框架 app.debug=True时,启动脚本被执行了2次
The first thing it does is start the main function in a new thread so it can monitor >the source files and restart the thread when they change.
http://stackoverflow.com/questions/9276078/whats-the-right-approach-for-calling-functions-after-a-flask-app-is-run
如果你想要避免加载两次,应该设置app.run(debug=True, use_reloader=False)
Image in bytes shouldn’t be put in Queue
If you put images in bytes to a Queue in Python, then you can’t get it. (error occurs at queue.get())
Solution: convert your image to ndarray using opencv
cv2.imdecode(np.frombuffer(IMAGE_IN_BYTES, np.uint8), -1)
CUDA initialization error
Solution: add the following line to your main()
multiprocessing.set_start_method(‘spawn’)