Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
How to save and edit server rendering data?

I am using Flask server with python.

I have an integer pics_to_show. Every time a request is recieved, the user recieves pics_to_show integer. And pics_to_show gets decremented by 1. pics_to_show is an integer that`s shared with all website users. I could make a database to save it, but I want something simpler and flexible. Is there any OTHER way to save this integer.

I made a class that saves such variables in a JSON file.


class GlobalSate:
    def __init__(self, path_to_file):
        self.path = path_to_file
        try:
            open(path_to_file)
        except FileNotFoundError:
            f = open(path_to_file, 'x')
            f.write('{}')

    def __getitem__(self, key):
        file = self.load_file()
        data = json.loads(file.read())
        return data[key]

    def __setitem__(self, key, value):
        file = self.load_file()
        data = json.loads(file.read())
        data[key] = value
        json.dump(data, open(self.path, 'w+'), indent=4)

    def load_file(self):
        return open(self.path, 'r+')

The class is over-simplified ofcourse. I initialize an instance in the __init__.py and import it to all routes files (I am using BluePrints).

My apllication is threaded, so this class might not work... Since multiple users are editing the data at the same time. Does anybody have another solution?

Note: The g variable would not work, since data is shared across users not requests.

Also, what if I want to increment such variable every week? Would it be thread safe to run a seperate python script to keep track of the date, or check the date on each request to the server?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You will definitely end up with inconsistent state, with out a locking mechanism between reads and writes, you will have race conditions. so you will loose some increments.

Also you are not closing the open file, if you do that enough times it will crash the application.

Also one good pice of advice is that you do not want to write a state managing software, (database) it is very very difficult to get it right.

I think in your situation the best solution is to use sqlite, as it is a lib that you call from your app, there is not additional server.

"I could make a database to save it, but I want something simpler and flexible"

In a multi threaded app you can not go simpler than sqlite, (if you want your app to be correct that is).

If you do not like SQL then there are some simpler options:

  1. zodb http://www.zodb.org/en/latest/guide/transactions-and-threading.html
  2. pikleDB https://github.com/patx/pickledb
  3. python shelve but you will need to use file system locks
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda