Forums

500 RequestError(s) with gspread

I once in a while get 500 request errors when running a script that writes output to a Google spreadsheet: The first one looks like this:

gspread.exceptions.RequestError: (500, '500: This server no longer owns the lock of [my db name]')

The second one prints out a large block of html along with the error message and part of it says "Google Docs encountered an error. Please try reloading this page, or coming back to it in a few minutes." The first error often goes away when I just run the script again but the second one sometimes persists.

Does anyone know if there's anything I can do to prevent these errors?

Sorry, that's beyond my gspread knowledge. Perhaps one of our users can help out.

I figured the error might have had something to do with the connection to the spreadsheet timing out. I learned there was a way to write faster cell updates with the gspread package so my code that writes to the spreadsheet should be a lot faster now, and so far I haven't seen either of these errors so I'm hoping that did the trick.

Cool- could you point us to any documentation / forum posts etc about this? (so other users with the same problem can referent that?)

The other thing that could be possible is just for you to re-get the spreadsheet again (I'm guessing you got the spreadsheet and then waited too long and then tried to insert/update)

I learned about faster cell updates here. The other problem was pretty much as you said - I was connecting well before I actually wrote anything to the spreadsheet and my credentials were being invalidated. I fixed that by following this. Scroll all the way down and there is a block of code that I used to figure out how to reconnect. However the code block:

if creds.access_token_expired:
      gs_client.login()  # refreshes the token

did not work for me, as I had a case where this if statement was False but I still disconnected afterwards while adding cells. I simply do gs_client.login() right before I write to the spreadsheet and so far this has worked.

Glad you worked out a solution, and thanks for sharing it!