Towernter
Towernter

Reputation: 275

Failure to edit a file hosted on Heroku using python

I have deployed my python script on Heroku. The script uses a for loop to iterate and it updates the file on every iteration. If the server goes down the script checks for the value stored in the file and it should continue the iteration from the point where it left. In this case, the script is restarting at 1 which is the value that I put when I deployed the script. If I run this script on my local machine and stop it is continuing from where it left of.

Below is the sample code

if __name__ == "__main__":
      with open('file.txt', 'r') as file:
           file_data = file.readlines()
           count = file_data
      for i in range(count, some_long_number):
           with open('file.txt', 'w') as file:
                file.writelines(str(i))
           time.sleep(900)

Upvotes: 0

Views: 23

Answers (1)

Damien MATHIEU
Damien MATHIEU

Reputation: 32627

Heroku has an ephemeral filesystem. A container (very similar to Docker) is built whenever you deploy your app. It is then executed at runtime.

This means any file changes will not be preserved across app restarts or redeployments, hence your issue here.

You need to store your file counter in a persistent database such as PostgreSQL or Redis. Heroku offers those as add-ons.

Upvotes: 1

Related Questions