Forums

Error code: 502-backend/504-loadbalancer due to machine learning model

Hello , I am just a beginner at using flask and am trying to set up a website as I wanted to add a machine learning model to it. When I press the button that runs the model function, it takes very long and usually ends up with the errors above. I understand that this means that the machine learning model takes too long, but is there a way to correct this error?

See our help page about using machine learning in web apps: https://help.pythonanywhere.com/pages/MachineLearningInWebsiteCode/

I have already switched to theano but when importing keras, they still say no module found named tensorflow. I have change the keras.json file to say backend = theano and have tried to set the environmental variable in the virtual env to theano as well but the error keeps coming up. Is there a way to change this?

Current version of keras = 2.3 Current version of numpy = 1.20.3

Hello, I have changed the machine learning model to use xgsboost instead. However, it is still experiencing the 504-loadbalancere. Is there a reason for this?

Look in your error and server logs, there may be clues there.

I have looked at the error logs and server logs. For the error logs, no error occurs when I press the button. But for the server log it just displays the harakiri:

2023-05-10 23:35:32 Wed May 10 23:35:27 2023 - *** HARAKIRI ON WORKER 2 (pid: 14, try: 1) ***
2023-05-10 23:35:32 Wed May 10 23:35:27 2023 - HARAKIRI !!! worker 2 status !!!
2023-05-10 23:35:32 Wed May 10 23:35:27 2023 - HARAKIRI [core 0] 10.0.0.83 - POST /predict since 1683761126
2023-05-10 23:35:32 Wed May 10 23:35:27 2023 - HARAKIRI !!! end of worker 2 status !!!
2023-05-10 23:35:32 DAMN ! worker 2 (pid: 14) died, killed by signal 9 :( trying respawn ...
2023-05-10 23:35:32 Respawned uWSGI worker 2 (new pid: 19)
2023-05-10 23:35:32 spawned 2 offload threads for uWSGI worker 2

Are you sure there are no errors in the error log corresponding to POST on /predict?

Yes, there are no errors in the error log. I just reloaded the website and tried to run the prediction again but I got this error. From the

Last Error Log line:
2023-05-11 00:12:44,684: OSError: write error

Last line in server Log:    
2023-05-11 12:00:27 admin
2023-05-11 12:00:27 bcc856@gmail.com

Last Line in Access Log
180.129.39.118 - - [11/May/2023:12:10:59 +0000] "POST /predict HTTP/1.1" 499 0 "http://unaginagi.pythonanywhere.com/home" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36" "180.129.39.118" response-time=299.998

I can't find any issues with my code as it runs on my local host fine. Is it because I am running it from Asia?

That request is taking too long, so the worker is being killed. See https://help.pythonanywhere.com/pages/AsyncInWebApps/ for how to offload long-running tasks.

I have tried to add in async but I do not notice any changes? Also, async doesn't really quicken the process right?

The approach suggested in the Async work in web apps help page may not speed up elapsed time of the whole process, but will divide one long process into multiple different processes and will offload the web app worker, so the requested view could load quickly with some waiting time for data to be calculated in the background.