SaveJSON()

Just starting out? Need help? Post your questions and find answers here.
User avatar
idle
Always Here
Always Here
Posts: 5886
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

SaveJSON()

Post by idle »

does SaveJSON lock the file for concurrent writes or do I have to lock the file myself?
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: SaveJSON()

Post by PBJim »

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.

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
User avatar
idle
Always Here
Always Here
Posts: 5886
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: SaveJSON()

Post by idle »

Thanks PBjim, that looks encouraging.
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: SaveJSON()

Post by PBJim »

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.
User avatar
jacdelad
Addict
Addict
Posts: 2003
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: SaveJSON()

Post by jacdelad »

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
User avatar
idle
Always Here
Always Here
Posts: 5886
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: SaveJSON()

Post by idle »

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.
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.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: SaveJSON()

Post by freak »

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
User avatar
idle
Always Here
Always Here
Posts: 5886
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: SaveJSON()

Post by idle »

Great 👍 thanks
Post Reply