It's the first time I use Heroku and it's ephemeral file system. I have a python script that requires a temporary folder to write some files while the script runs.
What I have done first: create a 'tmp' folder in my heroku root folder following this logic:
mkdir tmp
touch tmp/.keep
git add tmp/.keep
git commit -m "Add tmp directory to app repository."
I have also created a simple python script to store a dataframe into a text file:
import pandas as pd
df = pd.DataFrame( {'Name': {0: 'John', 1: 'Luc', 2: 'Steph'}, 'Gender': {0: 'Male', 1: 'Male', 2: 'Female'}, 'Age': {0: 55, 1: 47, 2: 20}} )
df.to_csv('tmp/df.txt')
In the Heroku CLI, I run that script with:
heroku run python createfile.py
After the script has run, I go to the 'tmp' folder using bash:
heroku run bash
cd tmp
ls
But the folder is empty, while I would expect the df.txt to be there. Is there any step I am missing? Or do I need a paying Heroku account to be able to write to the tmp folder? Do I have to chmod this folder first?
edit: I did some more tests, and it seems that the file gets correctly written to the tmp folder. It's just that it cannot be seen / gets deleted when using the 'heroku run bash' command.
CodePudding user response:
Heroku doesn't behave like a virtual server. By design, dynos are disposable.
Every time you use heroku run
you access a one-off dyno that is discarded as soon as the command ends (or you log out in the case of something interactive like bash
). You are running your createfile.py
script and bash
in two completely unrelated instances that do not share a filesystem.
I'm not clear on what you are trying to achieve, but in general you can keep temporary "working" things in the memory of a single process for a short period of time. Anything you wish to persist beyond that scope must be saved outside of your dyno, e.g. in a client-server database like PostgreSQL or an object store like Azure Blob Storage.
Note that this model is a deliberate architectural decision and it impacts all tiers of Heroku dynos. Upgrading to a paid plan won't change anything.