简体   繁体   中英

Call a function when Flask session expires

In my Flask application, I am saving files that correspond to a user, and want to delete these files when the user's "session" expires. Is it possible to detect the session expiration and immediately call a function?

Had the same issue and solved it not by using the in-built permanent session expiry feature, but added my own key to the session and checking it before every request like follows:

@app.before_request
def before_request()

    now = datetime.datetime.now()
    try:
        last_active = session['last_active']
        delta = now - last_active
        if delta.seconds > 1800:
            session['last_active'] = now
            return logout('Your session has expired after 30 minutes, you have been logged out')
    except:
        pass

    try:
        session['last_active'] = now
    except:
        pass

Yeah its kind of possible run a loop to see until session['key']==None and if the condition becomes true call the function. I hope this helps!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM