O
O
Ocean_772019-10-13 19:07:35
Python
Ocean_77, 2019-10-13 19:07:35

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

1 answer(s)
S
Sergey Gornostaev, 2019-10-13
@Ocean_77

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 question

Ask a Question

731 491 924 answers to any question