Forums

Re-occurring 500 Internal Server Error? Python Flask App

I am using the free python hosting to work on some personal python projects. I recently uploaded a flask web server. I am somewhat new to python, and flask, and not quite sure what the issue is. I am using a virtual environment of python 3.6.

When I launched my flask app, everything seems to be working as it should. However, the site will suddenly stop working and instead shows a 500 internal server error. It doesn't seem to be related to clicking on anything, or using any functionality on the website itself. It seems random.

But once I get the error, If I reload my web app, in the python anywhere control panel, the site seems to load back up, just fine. But usually within 10-30 mins the site will stop working again, and show the 500 internal server error.

When ever the site crashes, I get the following message in my error log. And the same message appears on my log whenever the site crashes.

2018-07-14 20:26:33,987: Exception on / [GET]#012Traceback (most recent call last):#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app#012    response = self.full_dispatch_request()#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request#012    rv = self.handle_user_exception(e)#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception#012    reraise(exc_type, exc_value, tb)#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise#012    raise value#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request#012    rv = self.dispatch_request()#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request#012    return self.view_functions[rule.endpoint](**req.view_args)#012  File "/home/{site name}/mysite/app.py", line 105, in index#012    year_dropdown_menu = year_dropdown_list()  # Creates drop down menu options#012  File "/home/{site name}/mysite/app.py", line 23, in year_dropdown_list#012    cursor.execute('SELECT DISTINCT YEAR(time) AS Year FROM bitcoin_data ORDER BY Year DESC')#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute#012    self.errorhandler(self, exc, value)#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler#012    raise errorvalue#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute#012    res = self._query(query)#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 412, in _query#012    rowcount = self._do_query(q)#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/MySQLdb/cursors.py", line 375, in _do_query#012    db.query(q)#012  File "/home/{site name}/python36_bitcoin/lib/python3.6/site-packages/MySQLdb/connections.py", line 276, in query#012    _mysql.connection.query(self, query)#012_mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

Based on your error message, it looks like you're executing a query on your database to get all of the years available in your data. Your OperationalError seems to indicate you've lost a connection to MySQL (which makes sense given your comment on the error happening every 10-30 minutes).

There are some good thoughts on this StackOverflow post: https://stackoverflow.com/a/14268418

You might want to consider creating an intermediate table to store the distinct years. Presumably, the available years aren't changing that often. In fact, it might be simplest to just use Python to generate a list of years rather than calling the database at all.

If your project is based on bitcoins (from your folder structure in the error), the results of that query will be at most 9 rows!

@Analyticsinc, thanks for the help! That makes sense, I will try just generating a list in python and see how that works.

However, Im curious as to why the other functions, which make calls to the MySQL database dont fail as well, any thoughts on that?

That suggests that that query is taking a really long time to run. How long does it take when you run it in a MySQL console?