Forums

Web apps not working properly

Hi, so I've got a flask app running on pythonanywhere and as of right now, it's quite buggy. But the problem is, when I test it on my local machine, it works just fine! The problem is rather not in the code but in pythonanywhere, as sometimes the data for my database which is stored as a dictionary in the main file and synced with a json file automatically just disappears and acts funny. I'd rather have an actual pythonanywhere moderator help me because I'm nearly certain it is a problem with the server. Thanks

You're using a paid account, that means that your web app uses 2 (or more) web workers (processes to handle the requests). That's a difference between your local environment and PythonAnywhere. If you're using a global state in your code, that will likely cause the issues you're describing. See this help page for more details.

Thank you for the swift response! (Sorry now I'm a little slow now) If I've understood this correctly, my web app is basically run on two programs of the same file open (workers). And if I try to set the data to something on one, the other one will be completely different as it stayed the same. Do you know if there is a simple way of having a shared global variable between the two open workers? Cheers

The usual solution is to keep the data in the database.

But my app IS the database…

Sorry, I really need a solution to this asap, as I can't continue developing my database further with this kind of errors popping up every single minute

It's not an error. Your worker processes are ephemeral by design and serve only to handle requests. You need some form of persistence. Standard way to do it is to use existing database. You can also write your data to a file.

Yeah, but my app IS the database, I am ALREADY writing it into a file just not as quickly. My website is hosted on github pages and I connected the database to it that would store accounts and other things. The data may be corrupted when several workers are writing at the same time + the performance will be lower when two workers are constantly writing things into a file every second. I just want a good solution of sharing a dictionary between those workers. If this sentence doesn't attract your attention just like the other messages look at the code:

dict = {"apple":0}


@app.get("/")
def get():
   dict['apple'] += 1
   return "Done!"

@app.get("/check")
def check():
    return dict['apple']

I the dictionary called dict is the one I want to be shared between workers - THE OBJECT. I've tried doing many things but none have worked. Creating another localhost flask server that would run parallelly with the main workers would also be a challenge, as 1: Sometimes I access the same data I just wrote literally 100ms afterwards and 2: the second flask app would probably be shut down for using up all the bought time. Please read this fully and consider how you are replying. If I don't get this to work in the next day, I might as well just quit PythonAnywhere :C

Cheers.

I don't understand what do you mean exactly by "my app is the database". From what you're describing it looks to me like you're trying to reinvent what database was (among other things) invented for. In your example, you want to share the dict object between two different processes, which, in fact, have their own dict objects keeping different state. So, I'd say, what you actually want to achieve is to share the state of the apple count, not the dict object itself. Also, you want that state to be protected from being altered by worker 1 when worker 2 increases the count (and vice versa). That looks exactly like job for a database: instead of having a dict in your web app, you'd rather have, say, Apples table in the db which would store their count. Then you need to ensure that the increase of the count of apples in the db is being performed in a single transaction (see, for example, this Stack Overflow discussion).

Thank you for the reply, but the thing is: my flask app is a DATABASE as I've said several times. Well, it is an API rather, the database is literally just the dictionary with all the data. I do not have an SQL one running anywhere, I'm using this flask app to store things. I know there should be an easy way to connect those I just don't know one. Is it possible to, for example, host the workers as processes that just accept requests and then redirect it to an always-on flask app? Let me just explain it quickly:


pafk's ultimate pc --------- Hey, I wanna log in --------> worker 1/2 on my main web app ---- copies the link and sends a request to the main flask app (the always-on task) ---------------> main flask app ------ Processes and sends data back --------------> worker 1/2 ------------ then it just returns the data ---------> pafk's ultimate pc happy with their data

Is this scheme somewhat manageable to what pythonanywhere can provide? Or are there any other solutions that can share a dict object between the workers? Cheers

I'm using this flask app to store things.

Flask app is not a place to store things. On every level it's not designed to be a place to store data. You need to store data somewhere else, outside of the code serving your API endpoint.

Can you provide any great suggestions for that if possible, please? I've spent a lot of time trying to find some but I can't work it out... Thanks

We have three different database solutions available on PythonAnywhere. On is SQLite that is just a file on disk (so it could be as slow as our nfs filesystem can be), other two are MySQL and Postgres (that is extra paid). What have you tried?

I found another possible way of sharing a dict between scripts - pyro4. I have some code written but I'm unsure how to be able to share an editable dict on it, here is the stackoverflow question for more details: https://stackoverflow.com/questions/72572277/how-to-change-keys-and-values-of-a-dict-in-pyro4

pyro4 will not work on PythonAnywhere and, if it did, a database would probably still be significantly simpler.