Spaces:
Paused
Paused
Commit
•
ef69b25
1
Parent(s):
a0dfa27
Refactor save_vote function to use a lock for thread safety
Browse files
main.py
CHANGED
@@ -68,13 +68,14 @@ app = FastAPI(lifespan=lifespan)
|
|
68 |
|
69 |
|
70 |
def save_vote(vote_entry):
|
71 |
-
with
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
|
80 |
@app.get("/", include_in_schema=False)
|
|
|
68 |
|
69 |
|
70 |
def save_vote(vote_entry):
|
71 |
+
with scheduler.lock:
|
72 |
+
with open(VOTES_FILE, "a") as file:
|
73 |
+
date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
74 |
+
vote_entry["timestamp"] = date_time
|
75 |
+
file.write(
|
76 |
+
json.dumps(vote_entry) + "\n"
|
77 |
+
) # Add a newline character after writing each entry
|
78 |
+
logger.info(f"Vote saved: {vote_entry}")
|
79 |
|
80 |
|
81 |
@app.get("/", include_in_schema=False)
|