Skip to content

Instantly share code, notes, and snippets.

@wpcarro
Created December 18, 2024 16:32
Show Gist options
  • Save wpcarro/3f6c6ea556116ba87beb342c8a7d3faa to your computer and use it in GitHub Desktop.
Save wpcarro/3f6c6ea556116ba87beb342c8a7d3faa to your computer and use it in GitHub Desktop.
Durable state
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