Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 323
moe assal Avatar asked Aug 10 '20 11:08

moe assal


1 Answers

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
like image 176
Tomasz Swider Avatar answered Oct 13 '22 11:10

Tomasz Swider