Loading large dataset once at app creation

Yes, you can use lifecycle hooks for this purpose. If sessions only need read access to the data (no writing), then the simplest thing to do is to store the data in some attribute of another module in the same directory as your main.py. Python’s module caching ensures that all sessions that import the module will be able to access the data. You can see this demonstrated in the spectrogram example. If you need to mediate concurrent writes, then you will be better off putting the data in some actual real database and letting sessions talk to the database. (Alternatively you will have to implement your own locking strategy.)

1 Like