U
U
User5472016-02-16 21:42:53
Flask
User547, 2016-02-16 21:42:53

Flask, how to store global state between processes?

The bottom line is this: I'm writing a simple browser game, a poker simulator.
The Flask framework is on the back, but you can change it to any other, it doesn’t matter.
According to the current plan, the state of the game is saved between requests in RAM. In addition, it is also necessary to constantly keep a rather voluminous table in memory for a quick calculation of the seniority of a card combination.
Very simplified, the logic is as follows:
1) The hand class has a static array of currently running hands.
2) Each distribution is an object with a unique number, which, when created, launches a function with the logic of the distribution itself in a separate thread.
3) In this function, an infinite loop is spinning with exit by condition (all players made a move, bets are equalized), at each iteration of which it is checked whether the user's action came from the front, and if so, further logic is executed and the move is transferred to the next user.
4) When a user performs an action on the front, the number of the hand is sent to the back, according to which the required hand is found in the global array of hands, and the user's move is sent to it, which is checked in step 3.
All front-back interaction via websockets.

While the application is being tested on the localhost, where the server runs in 1 process, everything works as it should. But in real conditions, when the server will be launched in several processes, it will no longer be possible to store the state in this way in global variables (well, or static class variables, it doesn’t matter).

The logic of the game itself is quite complicated so that you can keep the state of the distribution in the database and restore it with each request, again, the table for calculating combinations is loaded into memory with each request, complete nonsense.

Tell me, how do you usually solve such problems? If I do everything completely, completely wrong, I will also be glad to hear)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2016-02-16
@deliro

Not at all like that. It needs to be stored in a database. You want in an operative - store in Redis'ke.
How difficult?

P
pcdesign, 2016-02-17
@pcdesign

You can also store states in files.
After all, states are just a structure that would be deserialized (and vice versa) using

import pickle
"""
Сохраняем сложнную структуру в файл
"""

favorite_color = {"lion": "yellow", "kitty": "red", "aaa": [0, 1, 2]}
pickle.dump(favorite_color, open("id.txt", "wb"))


"""
Читаем из файла
"""

favorite_color = pickle.load(open("id.txt", "rb"))
print(favorite_color)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question