Home > Software design >  keeping a set content between program runs in python
keeping a set content between program runs in python

Time:05-23

In my program I am keeping of set of items, which I want to keep between different runs

So I thought of storing the set in a file and loading the set from the file upon starting the program(sounds ok?)

My main question is: what is the right way to update the file?

  1. write an extra item to the file everything I change the set?

  2. keep new added items in a list/set and write them into the file every few seconds maybe?

  3. something else?

i'm guessing between 1 and 2, 1 is better? i think while method 2 might be faster, i might lose data if the server goes down, am i right?

thanks!!

CodePudding user response:

(I'm sorry for my bad english). There is no reason to wait a while. It would be the most logical solution to keep the action you have done in an item/variable and update the file after checking it. Updating the file directly will tire your device even more.

CodePudding user response:

I'd suggest using pickle, which serializes and unserializes python data types.

Do you anticipate the program unexpectedly exiting? If not, you could write it at start and at close.

  • Related