Created
December 18, 2024 16:32
-
-
Save wpcarro/3f6c6ea556116ba87beb342c8a7d3faa to your computer and use it in GitHub Desktop.
Durable state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import os | |
import json | |
state = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
tmp = "/tmp/buffer.json" | |
dst = "/tmp/dump.json" | |
# periodically dump application state to disk atomically | |
while True: | |
with open(tmp, "w") as f: | |
f.write(json.dumps(state)) | |
f.flush() | |
os.fsync(f) | |
os.rename(tmp, dst) | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment