Answer the question
In order to leave comments, you need to log in
Will there be problems when writing to JSON from FLASK?
I am writing a small web project for the purpose of education.
There was such a question. Will I have problems if several users send the corresponding post request at the same time, which calls the function (it writes to the json file). After all, as I understand it, a new thread is allocated for each user and several threads will get access to the same file at once. Will some of the information be lost in this case?write_json(data['object'])
from flask import Flask, json, request
from config import secret,
from message_handler import write_json
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello from Flask!'
@app.route('/', methods=['POST'])
def processing():
data = json.loads(request.data)
if data['secret'] == secret:
write_json(data['object'])
return 'ok'
Answer the question
In order to leave comments, you need to log in
Flask is single-threaded, requests are handled by a pool of processes. But in any case, some kind of lock is needed when accessing shared data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question