inconsistent memory content

Hi, I have an issue with my flask web app that I'm not able to reproduce locally.

I don't have a database, my data is loaded from a file in a few dictionaries kept in memory. I'm not doing any multithreading myself and to my knowledge neither are my dependencies.

But when I add an entry to my dict in memory, I'm not able to retrieve it back consistently. I've added a few debugging logs: the id() of the dict, the thread id and the len() of the dict.

The result of multiple successive calls:

  • The thread id is always the same
  • The dict id() is always the same
  • The dict len() varies randomly between the old and new value

Any idea of what could cause this?

Your web app runs in multiple processes - one per worker. Any "global" variables will have the values that they were assigned in a particular worker, and that will not be available in other workers. If you need values that are consistent across workers, you will need to use external storage of some sort (like a database or a file on the filesystem)

Is there any way to reduce the number of workers to 1 for the time being?

Sure! I've updated the configuration for your site, and if you reload it now, it will come back up with one worker. Just let us know when you're ready for us to change it back.

Thanks! It fixes my problem as far as I can tell.

Can you provide me some information about how I could try to reproduce the issue locally? I've tried to launch my flask app with threaded=True but it's not enough.

hmm how did you launch your flask app locally?

I've tried a few variations of in a main after importing files that initialize my data.

Hmm, I think the problem is that using multithreading starts a number of threads within the same process, but they because they're in the same process, they all have access to the same memory. Flask's development server (the one you start with is quite different to production environments -- it's really only mean for basic testing.

The best way to test this thoroughly on your own machine would be to use a "production-style" environment -- that is, use one of the tools that someone would use to deploy it on a server. The specific one we use on PythonAnywhere would probably be excessively hard to configure, so I'd suggest using Gunicorn for testing. You can tell it to start up a number of worker processes from the command-line -- Flask's documentation on using it is pretty good.