简体   繁体   中英

running a python script once on deploy on heroku

I have flask app that uses geodis which has dependency on redis that acts as cache for city mapped to latitude and longitude from geodis .

I have this code that needs to be run just once on deployment of the flask web app on heroku,

from geodis.provider.geonames import GeonamesImporter
import geodis
fileName = os.path.split(geodis.__file__)[0] + "/data/cities1000.json"
importer = GeonamesImporter(fileName, os.getenv("REDIS_HOST"), os.getenv("REDIS_PORT"), 0)
importer.runimport()

How can I have it setup to run once on deployment?

i think one way is to use application initialization function.

if __name__ == "__main__":
    fileName = os.path.split(geodis.__file__)[0] + "/data/cities1000.json"
    importer = GeonamesImporter(fileName,
                                os.getenv("REDIS_HOST"),
                                os.getenv("REDIS_PORT"), 0)
    importer.runimport()
    app.run(host='0.0.0.0', port=app.config['PORT']) 

this would run it before creating the app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM