Reputation: 11
Issue am facing here is that in my code there are many functions which are dumping data in a json file simultaneously like,
""" Data of the JSON file is like
{"Channels": [],1:[],2:[]} """
def update_channels():
with open(file,'r') as f:
data = json.load(f)
data["Channels"].append(stuff)
with open(file,'w') as f:
json.dump(data,f)
def update_key(key_no):
with open(file,'r') as f:
data = json.load(f)
data[key_no].append(stuff)
with open(file,'w') as f:
json.dump(data,f)
These update functions are triggered by events and there can be cases where they are executed at the same time or within no time gap so, data of that JSON file can be overlapped causing loss of data.
How can I resolve this problem? Basically, this is for a discord bot and these functions will be triggered by discord events.
Thanks!
Upvotes: 0
Views: 174
Reputation: 198324
There are several ways to do this. One is to use a database, which will do access control for you. Another one is to use a lock. But the simplest one is to leave it to the kernel: write to a temporary file, then replace the original atomically. I.e. to write a file, do this instead:
from tempfile import NamedTemporaryFile
import os
dirname = os.dirname(file)
with NamedTemporaryFile(dir=dirname, delete=False) as temp:
json.dump(data, temp)
temp.close()
os.replace(temp.name, file)
Python guarantees that nothing will interrupt os.replace
.
EDIT: This merely answers the question as posed; but I fully agree with comments saying a database is the right choice.
Upvotes: 2