SaveJSON()
SaveJSON()
does SaveJSON lock the file for concurrent writes or do I have to lock the file myself?
Re: SaveJSON()
If it's any help Idle, I had the same question when I developed a multi-user application that services requests to save JSON data. I carried out some tests by writing from multiple threads and I found that if another thread was in the process of writing the same JSON file, then the Else clause was performed. I think the answer therefore is that it's a lock, with the Else being executed.
This is the test below — just a loop. I'm sure the developers can confirm more detail, but thought these results might be of relevance.
List saved as file "test" records 1
List not saved
List saved as file "test" records 1
List not saved
List not saved
List saved as file "test" records 1
List not saved
List saved as file "test" records 1
List saved as file "test" records 1
List not saved
List saved as file "test" records 1
This is the test below — just a loop. I'm sure the developers can confirm more detail, but thought these results might be of relevance.
Code: Select all
If SaveJSON(testno.i, filename.s)
Else
; List not saved
EndIf
List saved as file "test" records 1
List not saved
List saved as file "test" records 1
List not saved
List not saved
List saved as file "test" records 1
List not saved
List saved as file "test" records 1
List saved as file "test" records 1
List not saved
List saved as file "test" records 1
Re: SaveJSON()
Thanks PBjim, that looks encouraging.
Re: SaveJSON()
Yes, it's pretty robust. I was more concerned that the app might crash, but it didn't. I ran four threads, each 100,000 loop iterations, attempting to write to the same file.
The only thing is, if you absolutely need to create the file, it's necessary to wrap it inside a re-try loop, which then makes the code seem a bit over-engineered, just for the sake of writing a file.
The only thing is, if you absolutely need to create the file, it's necessary to wrap it inside a re-try loop, which then makes the code seem a bit over-engineered, just for the sake of writing a file.
Re: SaveJSON()
Use a mutex to queue the write operations.
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: SaveJSON()
I'm defiantly guilty of over engineering. I also need to protect the file across processes. I'm using a lock file for that but haven't' tested it yet.PBJim wrote: Thu Jun 20, 2024 11:16 am Yes, it's pretty robust. I was more concerned that the app might crash, but it didn't. I ran four threads, each 100,000 loop iterations, attempting to write to the same file.
The only thing is, if you absolutely need to create the file, it's necessary to wrap it inside a re-try loop, which then makes the code seem a bit over-engineered, just for the sake of writing a file.
Re: SaveJSON()
The file is opened with FILE_SHARE_READ mode only so it cannot be opened for writing at the same time but it can be read.
quidquid Latine dictum sit altum videtur
Re: SaveJSON()
Great
thanks