Forums

Strange python behavior in Flask...

I have a relatively simple, single user (only me so far) Flask app with some code that looks like this (not exact code but very close):

Foo = { someKey: someValue }
## then
def Register(user):
    global Foo
    ... someKey = Do Stuff ...

    Foo[someKey] = some string value

...

@app.route('/addUser')
...
    Register(user)

...

def isRegistered(user):
    global Foo

    if user in Foo:
        return True
    return False

...

@app.route('/checkUser')
...
    if isRegistered(user):
...

This works just fine on python on my local Flask server (python 3.9) as well as in other production scenarios. Foo has the key added by Register() and it's found in the subsequent isRegistered() call. The other thousand or so lines in this file work exactly as expected. I have written tons of similar code - it always works as expected. I could believe a typo or missing global or something but the code works elsewhere.

I tried this with Foo as an array pushing things into it - same result - later in isRegistered() Foo is empty.

In the dictionary case the initial value (someKey) in Foo persists - only the later Register() additions disappear.

I also wrapped read/write of Foo with a mutex. No change in behavior.

There are no errors or warnings I can find in any log.

Only the additions added later during Flasks operation are not there... initial values persist.

The ONLY explanation I can think of is somehow whatever thread/co-routine Flask creates to handle the requested /addUser is dying silently - though I see no reason why.

Are there any other ways to debug this?

[formatted by admin]

I eventually figured out it was Flask and thread separation...

Glad to hear you solved that!

Friend, do you want to tell me that when I type these codes in my app.py file I won't have any more problems after trying to log in to my website?

What code do you mean?