Add thread lock

This commit is contained in:
Jose De Freitas 2021-03-07 16:26:11 -05:00
parent e01cf285c8
commit bf8a5ca942
3 changed files with 9 additions and 5 deletions

View File

@ -4,7 +4,7 @@
"Don_Jones": 0,
"Corey_Schafer": 0,
"Brian_Will": 0,
"LearningLad": -7,
"LearningLad": 0,
"David_Bombal": 0,
"Ben_Eater": 0,
"The_Coding_Train": 0,

View File

@ -1,4 +1,5 @@
import json
import threading
from flask import Flask, jsonify, make_response, request
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
@ -8,12 +9,14 @@ app = Flask(__name__)
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=["1200 per day", "50 per hour"]
default_limits=["720 per day", "30 per hour"]
)
with open("data.json", "r", encoding="utf8") as read_data:
channels = json.load(read_data)
lock = threading.Lock()
@app.route("/")
def main():
@ -49,8 +52,9 @@ def get_channel(channel):
return "Vote word not recognised."
# Write to database file.
with open("data.json", "w", encoding="utf8") as write_data:
json.dump(channels, write_data, indent=4)
with lock:
with open("data.json", "w", encoding="utf8") as write_data:
json.dump(channels, write_data, indent=4)
return f"You {vote}d successfully the channel {channel}."
else:

View File

@ -32,4 +32,4 @@ video quality, coverage of the topics it explains, among others), but, of course
so I think being able to receive opinions from all users is a good way to filter and have truly
awesome content based on the community's opinion.
<img src="http://127.0.0.1:5000/channels/Tech_With_Tim/image.svg">
<img src="https://awesome-youtubers.herokuapp.com//channels/3Blue1Brown/image.svg">