save_plant: inelegant concurrency fix

This inelegant and hackyish fix gives a randomly generated name to the
temporary savefiles, so there is only a 0.1% chance of the filename
being the same twice in a row.

This solves the ocasional exceptions raised in save_plant, when there
is more than one call still running at once, for the same user (due to
slow writes).
pull/38/head
Marcos Marado 2021-02-26 22:50:12 +00:00
parent 38f7f17b66
commit 8d2e53ad83
1 changed files with 1 additions and 1 deletions

View File

@ -561,7 +561,7 @@ class DataManager(object):
def save_plant(self, this_plant): def save_plant(self, this_plant):
# create savefile # create savefile
this_plant.last_time = int(time.time()) this_plant.last_time = int(time.time())
temp_path = self.savefile_path + ".temp" temp_path = self.savefile_path + ".temp" + str(random.randint(0,1000))
with open(temp_path, 'wb') as f: with open(temp_path, 'wb') as f:
pickle.dump(this_plant, f, protocol=2) pickle.dump(this_plant, f, protocol=2)
os.rename(temp_path, self.savefile_path) os.rename(temp_path, self.savefile_path)